python 進程池Pool

來源:互聯網
上載者:User

標籤:

 1 #-*- coding:utf-8 -*- 2 from multiprocessing import Pool 3 import os,time,random 4  5 def long_time_task(name): 6     print ‘Run task name %s and pid : %s..‘%(name,os.getpid()) 7     start = time.time() 8     time.sleep(random.random()*3) 9     end = time.time()10     print ‘Task %s betwine %s ‘%(name,(end-start))11 #    pass12 if __name__ == ‘__main__‘:13     print ‘Parent process %s.‘%os.getpid()14     p=Pool()15     for i in range(5):16         print ‘now I is ‘ and i17         print ‘Parent process %s.‘%os.getpid()18         p.apply_async(long_time_task,args=(i,))19     print ‘Wait for all subprocesses done...‘20     print (i,)21     p.close()22     p.join()23     print ‘All over‘

結果(類):

Parent process 3896.
0
Parent process 3896.
1
Parent process 3896.
2
Parent process 3896.
3
Parent process 3896.
4
Parent process 3896.
Wait for all subprocesses done...
(4,)
Run task name 0 and pid : 4300..
Run task name 1 and pid : 4596..
Task 1 betwine 1.32100009918
Run task name 2 and pid : 4596..
Task 0 betwine 2.61500000954
Run task name 3 and pid : 4300..
Task 2 betwine 2.65400004387
Run task name 4 and pid : 4596..
Task 4 betwine 0.599999904633
Task 3 betwine 2.77799987793
All over



 

python 進程池Pool

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.