bioscrape icon indicating copy to clipboard operation
bioscrape copied to clipboard

Add Multiprocessing example

Open WilliamIX opened this issue 4 years ago • 3 comments

To do a parameter sweet and to run many stochastic simulations and average the results to get a distribution.

WilliamIX avatar Apr 28 '20 21:04 WilliamIX

One gotcha to watch out for -- I've found that using multiprocessing correctly usually requires OS-specific tweaks. Make sure any multiprocessing examples work on, at minimum, Windows 10 and some reasonable OSX.

sclamons avatar Apr 03 '21 02:04 sclamons

@sclamons what tweaks do you know about for Mac OSX? I've only done this on Windows and Linux which function the same.

WilliamIX avatar Apr 03 '21 17:04 WilliamIX

I have to do

if not multiprocessing.get_start_method() == "spawn":
            multiprocessing.set_start_method('spawn')

or the new processes hang. The guard is there because if you try to set_start_method when the start method is already safe, multiprocessing throws an exception. This is a known bug in Linux, too.

Another problem, this one specific to IPython notebooks: IPython can't pass functions defined in a notebook to a function like Pool.map, for reasons I really don't understand. If you want to wrap your lineage simulation in anything more complex than the built-in bioscrape simulation methods, you have to write that function in a separate file and import it. I've used the following code to write a notebook-defined function simulate_single_lineage into a temporary file and import it.

        file.write(inspect.getsource(simulate_single_lineage))
try:
    importlib.reload(tmp_func)
except:
    import tmp_func```

sclamons avatar Apr 03 '21 19:04 sclamons