Closed jessexknight closed 2 years ago
Okay, I have a workaround from here: if you import matplotlib.pyplot as plt
within the plotting function, it works:
from pathos.multiprocessing import ProcessingPool as pmp
def plotfun(i):
import matplotlib.pyplot as plt
plt.plot([0,i],[0,i])
plt.savefig('tmp-{}.pdf'.format(i))
plt.close()
vec = [1,2,3]
# for i in vec: plotfun(i) # OK
pmp().map(plotfun,vec)
Importing is kinda ugly, and if you have multiple modular plot functions, maybe importing plt
multiple times breaks things? Anyways, with help from here you can decorate the plot function(s) to import plt
only once as needed:
def iplt(fun):
def decorator(*args,**kwds):
if 'plt' not in fun.__globals__:
import matplotlib.pyplot as plt
fun.__globals__['plt'] = plt
result = fun(*args,**kwds)
return result
return decorator
Usage:
@iplt
def plotfun(i):
...
It's good practice to make sure that any function that you plan to run in parallel has full encapsulation (i.e. all variables used are defined inside the function). So, imports should go inside the function definition. There is no harm in having an import executed twice (unless you are importing *
).
Let me know what you are looking to get answered in this issue, or if you have resolved it yourself.
Thanks -- I think you can close the issue, as the local import workaround is very reasonable. Plus, from this and this matplotlib issues, it seems the root cause may be upstream related to different backends (as shown below)...
If I run the MWE with:
import matplotlib
matplotlib.use('<<backend>>')
with <<backend>>
as any of: agg
, cairo
, pdf
, pgf
, ps
, svg
, template
, the MWE works. I didn't test all backends as I didn't want to install all packages, and frankly I'm not clear what the differences are, but leaving this here in case it's helpful...
Nice follow-up. Thanks for posting.
MWE
Errors:
First, this appears, and it hangs:
If I CTRL+C, I get:
One time, randomly it seems, I also got this error message appended to the first error:
Additional Info:
OS: Linux Mint 20.1 DE: X-Cinnamon / Muffin
Thanks in advance!
PS: #153 may be related (and also this matplotlib issue), although those seem related to fonts and the fix didn't help, nor did
plt.close()
, evidently above...