【发布时间】:2020-09-03 00:04:27
【问题描述】:
我正在尝试更熟悉 concurrent.futures,以便我可以对一些更复杂的任务进行并行处理。要了解我只是想在 python(spyder 解释器)中执行这段代码:
import concurrent.futures
import time
start = time.perf_counter()
def do_something(seconds):
print(f'Sleeping {seconds} second(s)...')
time.sleep(seconds)
return f'Done Sleeping...{seconds}'
if __name__ =='__main__':
with concurrent.futures.ThreadPoolExecutor() as executor:
secs = [5, 4, 3, 2, 1]
results = executor.map(do_something, secs)
# for result in results:
# print(result)
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')
我得到了我期望的输出:
runfile('D:/untitled1.py', wdir='D:/MarketProject')
Sleeping 5 second(s)...
Sleeping 4 second(s)...
Sleeping 3 second(s)...
Sleeping 2 second(s)...
Sleeping 1 second(s)...
Finished in 5.0 second(s)
但是当我将 'concurrent.futures.ThreadPoolExecutor()' 更改为 concurrent.futures.ProcessPoolExecutor() 我得到的只是
runcell(0, 'D:/untitled1.py')
Finished in 0.12 second(s)
了解为什么在尝试使用进程而不是线程时它不起作用?
【问题讨论】:
标签: python multithreading multiprocessing