Search code examples
pythonmultiprocessingpython-multiprocessing

Strange Behavior of Multiprocess Manager Ignoring Rest of Code Block


I am experiencing strange behaviors regarding Manager, where the code from the same code block after it's instantiation gets ignored.

I am running this on Windows 10.

When I run only the following code on a separate test.py file, everything works fine. Manager() does create it's own proxy process, and all print() functions executes. Also, runIP process doesn't seem to have any problem.

test.py

if __name__ == "__main__":
    from Image_Processing.image_processing import runIP
    from multiprocessing import Manager
    print("TRYING TO CREATE MANAGER")
    manager = Manager()
    print("CREATED MANAGER")
    runIP(manager)
print("ALL DONE!")

output

TRYING TO CREATE MANAGER
CREATED MANAGER
ALL DONE!

However, when I run the same code in main.py, where it has codes that import tensorflow, it goes weird.

main.py

if __name__ == "__main__":
    from Image_Processing.image_processing import runIP
    from multiprocessing import Manager
    print("TRYING TO CREATE MANAGER")
    manager = Manager()
    print("CREATED MANAGER")
    runIP(manager)
print("ALL DONE!")


from queue import Queue
from threading import Thread
from stable_baselines import DQN
import Reinforcement_AI.env.seperate_env as sep_env
...

output

TRYING TO CREATE MANAGER
ALL DONE!

# tensorflow import logs printing

The second print() and runIP() does not execute, however, by checking the Task Manager, I did confirm that the proxy process is running. The python interpreter just ignores the rest of the code block for some reason.

I have no clue what's causing this behavior. This is my first time attempting multiprocessing in Python, so I might be doing something wrong.


Solution

  • It turns out Manager has a problem with tensorflow.

    To make sure Manager is instantiated before doing anything with tensorflow, move every code related to tensorflow into a funcion, including the imports.
    Then call the funcion in the if __name__ == "__main__":.

    def launchAgent():
        from stable_baselines import DQN
        import Reinforcement_AI.env.seperate_env as sep_env
        from queue import Queue
        from threading import Thread
    
        #tf related code
        ...
    
    if __name__ == "__main__":
        from Image_Processing.image_processing import runIP
        from multiprocessing import Manager
    
        print("TRYING TO CREATE MANAGER")
        manager = Manager()
        print("CREATED MANAGER")
        runIP(manager)
        launchAgent()
    

    This solved my issue.