Skip to content Skip to sidebar Skip to footer

Importing Keras Breaks Multiprocessing

While using keras I found that I couldn't use multiprocessing.Pool. After some troubleshooting I think importing keras is the source of the problem and have created a simple exampl

Solution 1:

Here is a possible solution to this problem that worked for me since the child processes don't require keras to be imported.

if __name__ != '__mp_main__': #This line solves the problem
    import keras
from multiprocessing import Pool

def foo(q,y):
    print("In foo")
    return q,y
def test(a, b):
    x = []
    if __name__ == '__main__':
        p = Pool(5)
        print("Starting")
        x = p.starmap(foo, [[a,2],[b,4]])
        print("Finished")
        p.close()
        p.join()
    print(x)

if __name__ == '__main__':
    test(1,3)
    print(keras.backend)

How I found this solution was very weird. I ran the original code and it hung as normal. Then even as it was hung I commented out the import line and saved the file for the next test. As soon as I did this the code finished executing somehow. I tested this several times to make sure it was no fluke. How can editing the file of an already run program affect its operation? I then figured the if __name__ == '__main__' might be similar to if(fork() == 0) in c so I put it around the import so the children processes would not run it. Just as if I was manually commenting it out and saving during the run time. I tested it and it works.

Edit: If you use if __name__ == __main__ then inheritance gets broken since they aren't main it seems. mp_main is the name of the pool workers and this works as well


Post a Comment for "Importing Keras Breaks Multiprocessing"