我正在尝试从 Python 中的并发期货中与 Process Pool Executor 并行运行一堆进程。
这些进程都在一个while循环中并行运行,这很棒,但由于某种原因,main方法之外的代码重复运行。我看到另一个答案说使用 name == main check 来修复,但它仍然不起作用。
有什么想法可以让我在 main 方法中运行代码吗?我的对象不断被重置。
编辑:我改为使用 ThreadPoolExecutor 运行我的代码并解决了问题,尽管我仍然对此感到好奇。
import concurrent.futures
import time
from myFile import myObject
obj = myObject()
def main():
with concurrent.futures.ProcessPoolExecutor() as executor:
while condition:
for index in range(0,10):
executor.submit(obj.function, index, index+1)
executor.submit(obj.function2)
time.sleep(5)
print("test")
if __name__ == "__main__":
main()