同异步调用shutdown参数

Posted 似水年华梦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同异步调用shutdown参数相关的知识,希望对你有一定的参考价值。

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参数的主要内容,如果未能解决你的问题,请参考以下文章

如何从片段 KOTLIN 中调用意图 [重复]

shutdown()的评注

register_shutdown_function 函数详解

shutdown命令

ASP.net MVC 代码片段问题中的 Jqgrid 实现

jasmine.objectContaining 的单步调试