Dash with multiprocessing

Whenever I call a function that uses multiprocessing I get runtime error like this:

Runtime Error: No root path can be found for the provided module "__mp_main__". This can happen because 
the module came from an import hook that does not provide file name information or because it's a namespace 
package. In this case the root path needs to be explicitly provided.

I suppose it can be connected to the fact that there is no forking in windows (or not. I really know very little about it), but I have no idea what should be changed.
This function works fine when called from a simple script (with if __name__ == "__main__", of course), but this doesn’t help with dash.

Looking at this SO post, it looks like you could be right. It might have something to do with the way multiprocessing works in Windows, where the lack of forking means worker processes have to create a new process and re-import the parent module, but not using the value __main__. I can’t tell what that means for your app though. Perhaps a minimally reproducible example would help.

I started to write an example and, wonderfully, it worked. Trying to find the difference, I went through my code line by line to understand that this error is rising when I import some functions from scipy (i.e. scipy.optimize.brute or scipy.stats.norm). Other functions from other modules – fine. Some other functions from scipy (i.e. from scipy.fftpack) – fine, but these two caused this error.
In my case, I haven’t actually needed them that much, so I just dropped them and everything started to work perfectly.
But anyway, this behavior seems really strange. Maybe you have some idea why this happens?

I am getting the same error if I am using the following code
pool = Pool(cores)

In short my understanding is, if I am using parallel processing to process my dataframe, its throwing error, else its working fine.
Without this line, the code works perfectly. I am not able to figure out what is the issue.
Any help please!!!

I have the same problem. Any idea?

In python the if name == “main” is a condition that only gets executed when the file (let’s call it main.py) is the parent process, so if you import something inside the main.py from a different file it doesn’t export what’s below that condition. So when working with multiprocessing what is happening most probably is that you have the declaration of your app outside of the main, but the .run_server inside of it.
Solution? Have your main.py and the declaration of your app in different files. i.e:

app_declaration.py:

import dash

my_app = dash.Dash(name)

main.py
from app_declatation import my_app

if name == “main”:
my_app.run_server(host=“localhost”, port=8080, debug=True)

1 Like

Interesting proposition - how would the multiprocessing code fit in there? It has to be in the guard of the main python file, else it wouldn’t work. I’m not too sure how the access the return value of the multiprocessing code, would greatly appreciate if you could shed some light on this.