0

I'm trying to run a bunch of processes in parallel with the Process Pool Executor from concurrent futures in Python.

The processes are all running in parallel in a while loop which is great, but for some reason the code outside of the main method repeatedly runs. I saw another answer say to use the name == main check to fix but it still doesn't work.

Any ideas how I can just get the code inside the main method to run? My object keeps getting reset repeatedly.

EDIT: I ran my code using ThreadPoolExecutor instead and it fixed the problem, although I'm still curious about this.

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
zohani
  • 53
  • 5
  • Each process receives a copy of the object (in fact the entire program in many cases) and operates on that - you can't just share any regular object between separate processes, you'll need to do more work to communicate between the processes if such communication is required. (we can't tell, since there's no code here for `obj.function` and `obj.function2`). The reason it does work for threads instead of processes is that they can access variables across threads (within the same process), although that's not necessarily safe to do without problems. – Grismar Jan 07 '22 at 00:30
  • Makes sense, thanks! – zohani Jan 07 '22 at 00:33

0 Answers0