最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python多進(jìn)程使用及線程池的使用方法代碼詳解

 更新時間:2018年10月24日 08:49:33   作者:火鳥網(wǎng)絡(luò)  
這篇文章主要介紹了python多進(jìn)程使用及線程池的使用方法代碼詳解,需要的朋友可以參考下

多進(jìn)程:主要運行multiprocessing模塊

import os,time
import sys
from multiprocessing import Process
class MyProcess(Process):
  """docstring for MyProcess"""
  def __init__(self, arg, callback):
    super(MyProcess, self).__init__()
    self.arg = arg
    self.callback = callback
  def run(self):
    self.callback(self.arg)
def test(arg):
  print("子進(jìn)程{}開始>>> pid={}".format(arg,os.getpid()))
  for i in range(1,5):
    sys.stdout.write("子進(jìn)程{}運行中{}\r".format(arg,i))
    sys.stdout.flush()
    time.sleep(1)
def main():
  print("主進(jìn)程開始>>> pid={}".format(os.getpid()))
  myp=MyProcess(1,test)
  myp.start()
  myp2=MyProcess(2,test)
  myp2.start()
  myp.join()
  myp2.join()
  print("主進(jìn)程終止")
if __name__ == '__main__':
  main()

線程池:主要運用了未來模塊!下面例子,第一個是正常,第二第線程池,第三個用運行了2個線程池,會排隊

from concurrent.futures import ThreadPoolExecutor
import time
def sayhello(a):
  print("hello: "+a)
  time.sleep(2)
def main():
  seed=["a","b","c"]
  start1=time.time()
  for each in seed:
    sayhello(each)
  end1=time.time()
  print("time1: "+str(end1-start1))
  start2=time.time()
  with ThreadPoolExecutor(3) as executor:
    for each in seed:
      executor.submit(sayhello,each)
  end2=time.time()
  print("time2: "+str(end2-start2))
  start3=time.time()
  with ThreadPoolExecutor(2) as executor1:
    executor1.map(sayhello,seed)
  end3=time.time()
  print("time3: "+str(end3-start3))
if __name__ == '__main__':
  main()

總結(jié)

以上所述是小編給大家介紹的python多進(jìn)程使用及線程池的使用方法代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論

伊春市| 衡南县| 荥经县| 淮阳县| 白沙| 青田县| 绿春县| 砀山县| 安仁县| 汨罗市| 马尔康县| 教育| 桐柏县| 崇明县| 海伦市| 黑河市| 寿阳县| 旅游| 长白| 正镶白旗| 通河县| 阿拉善右旗| 永州市| 清流县| 丹东市| 湖口县| 文安县| 易门县| 沂南县| 库尔勒市| 万山特区| 湄潭县| 香河县| 祁门县| 舟曲县| 贵港市| 台安县| 长兴县| 渝北区| 晋城| 城口县|