Why ProcessPoolExecutor working serially?

扶醉桌前 提交于 2020-08-09 09:07:17

问题


from concurrent.futures import ProcessPoolExecutor
import os
import time

def parInnerLoop(item):
    print(f'Processing {os.getpid()} started on {item}')
    time.sleep(3)
    print(f'Processing {os.getpid()} done on {item}')

def main():
    executor = ProcessPoolExecutor(max_workers=4)

    for itemNo in range(10):
        executor.submit(parInnerLoop(itemNo))

if __name__ == '__main__':
    main()

What I'm trying to achieve is parallel for loop, similar to MatLab, e.g.:

parfor itemNo = 0:9
    parInnerLoop(itemNo);
end

What I'm getting: all os.getpid are same, and execution occurs serially. Any help?

Windows, VSCodium/VSCode, Python 3.7.3


回答1:


As mentioned by @Klaus, need to change from executor.submit(parInnerLoop(itemNo)) to executor.submit(parInnerLoop, itemNo).



来源:https://stackoverflow.com/questions/61432176/why-processpoolexecutor-working-serially

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!