欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 線程池用法簡單示例

 更新時(shí)間:2019年10月02日 11:41:01   作者:kevin.Xiang  
這篇文章主要介紹了Python 線程池用法,結(jié)合簡單實(shí)例形式分析了Python線程池相關(guān)使用技巧與操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Python 線程池用法。分享給大家供大家參考,具體如下:

# -*- coding:utf-8 -*-
#! python3
'''
Created on 2019-10-2
@author: Administrator
'''
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import os,time,random
def task(n):
  print('%s is runing' %os.getpid())
  time.sleep(random.randint(1,3))
  return n**2
if __name__ == '__main__':
  executor=ProcessPoolExecutor(max_workers=3)
  futures=[]
  for i in range(11):
    future=executor.submit(task,i)
    futures.append(future)
  executor.shutdown(True)
  print('+++>')
  for future in futures:
    print(future.result())

運(yùn)行結(jié)果:

38704 is runing
38704 is runing
38704 is runing
38696 is runing
38696 is runing
38696 is runing
38696 is runing
38696 is runing
38712 is runing
38712 is runing
38712 is runing
+++>
0
1
4
9
16
25
36
49
64
81
100

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python進(jìn)程與線程操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》、《Python+MySQL數(shù)據(jù)庫程序設(shè)計(jì)入門教程》及《Python常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論