码迷,mamicode.com
首页 > 其他好文 > 详细

同异步调用、shutdown参数

时间:2018-05-13 21:04:33      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:roc   OLE   任务   ESS   shutdown   stop   spool   from   调用   

1.使用shutdown

ex.shutdown(wait=True)是进程池内部的进程都执行完毕,才会关闭,然后执行后续代码 
如果改成false呢?看如下代码

from concurrent.futures import ProcessPoolExecutor
import  time
def task(name):
    print("name",name)
    time.sleep(1)

if __name__ == "__main__":
    start = time.time()
    ex = ProcessPoolExecutor(2)

    for i in range(5):
        ex.submit(task,"safly%d"%i)
    ex.shutdown(wait=False)

    print("main")
    end = time.time()
    print(end - start)

输出如下:

1 main
2 0.01500844955444336
3 name safly0
4 name safly1
5 name safly2
6 name safly3
7 name safly4

使用submit同步调用

同步调用:提交/调用一个任务,然后就在原地等着,等到该任务执行完毕拿到结果,再执行下一行代码

 1 from concurrent.futures import ProcessPoolExecutor
 2 import time, random, os
 3 
 4 def piao(name, n):
 5     print(%s is piaoing %s % (name, os.getpid()))
 6     time.sleep(1)
 7     return n ** 2
 8 
 9 
10 if __name__ == __main__:
11     p = ProcessPoolExecutor(2)
12     start = time.time()
13     for i in range(5):
14         res=p.submit(piao,safly %s %i,i).result() #同步调用
15         print(res)
16 
17     p.shutdown(wait=True)
18     print(, os.getpid())
19 
20     stop = time.time()

输出如下:

E:\python\python_sdk\python.exe "E:/python/py_pro/4 进程池.py"
safly 0 is piaoing 12996
0
safly 1 is piaoing 14044
1
safly 2 is piaoing 12996
4
safly 3 is piaoing 14044
9
safly 4 is piaoing 12996
1612932
5.202786684036255

Process finished with exit code 0

使用submit异步调用

异步调用: 提交/调用一个任务,不在原地等着,直接执行下一行代码

 1 # from multiprocessing import Process,Pool
 2 from concurrent.futures import ProcessPoolExecutor
 3 import time, random, os
 4 
 5 def piao(name, n):
 6     print(%s is piaoing %s % (name, os.getpid()))
 7     time.sleep(1)
 8     return n ** 2
 9 
10 
11 if __name__ == __main__:
12     p = ProcessPoolExecutor(2)
13     objs = []
14     start = time.time()
15     for i in range(5):
16         obj = p.submit(piao, safly %s % i, i)  # 异步调用
17         objs.append(obj)
18 
19     p.shutdown(wait=True)
20     print(, os.getpid())
21     for obj in objs:
22         print(obj.result())
23 
24     stop = time.time()
25     print(stop - start)

输出结果:

E:\python\python_sdk\python.exe "E:/python/py_pro/4 进程池.py"
safly 0 is piaoing 1548
safly 1 is piaoing 7872

safly 2 is piaoing 1548
safly 3 is piaoing 7872


safly 4 is piaoing 15487808
0
1
4
9
16
3.202626943588257

 

同异步调用、shutdown参数

标签:roc   OLE   任务   ESS   shutdown   stop   spool   from   调用   

原文地址:https://www.cnblogs.com/dingyutao/p/9033173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!