为什么 ProcessPoolExecutor 串行工作?
Why ProcessPoolExecutor working serially?
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()
我想要实现的是并行 for
循环,类似于 MatLab,例如:
parfor itemNo = 0:9
parInnerLoop(itemNo);
end
我得到的结果:所有 os.getpid
都相同,并且执行顺序发生。有帮助吗?
Windows, VSCodium/VSCode, Python 3.7.3
如@Klaus 所述,需要从 executor.submit(parInnerLoop(itemNo))
更改为 executor.submit(parInnerLoop, itemNo)
。
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()
我想要实现的是并行 for
循环,类似于 MatLab,例如:
parfor itemNo = 0:9
parInnerLoop(itemNo);
end
我得到的结果:所有 os.getpid
都相同,并且执行顺序发生。有帮助吗?
Windows, VSCodium/VSCode, Python 3.7.3
如@Klaus 所述,需要从 executor.submit(parInnerLoop(itemNo))
更改为 executor.submit(parInnerLoop, itemNo)
。