【问题标题】:python concurrent.futures thread vs. process questionpython concurrent.futures线程与进程问题
【发布时间】: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


    【解决方案1】:

    我之前在 Windows 上使用 Jupyter 笔记本和 Python 终端时遇到过这个问题。您定义的功能不适用于每个子进程,因此每个子进程都会立即死亡。解决方案是在单独的文件中定义函数并导入它,然后尝试使用该导入的函数进行映射。

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2010-09-12
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      相关资源
      最近更新 更多