Python rpy2 and matplotlib conflict when using multiprocessing

前端 未结 5 1810
误落风尘
误落风尘 2021-02-07 14:14

I am trying to calculate and generate plots using multiprocessing. On Linux the code below runs correctly, however on the Mac (ML) it doesn\'t, giving the error below:



        
5条回答
  •  误落风尘
    2021-02-07 14:57

    This error occurs on Mac OS X when you perform a GUI operation outside the main thread, which is exactly what you are doing by shifting your plot function to the multiprocessing.Pool (I imagine that it will not work on Windows either for the same reason - since Windows has the same requirement). The only way that I can imagine it working is using the pool to generate the data, then have your main thread wait in a loop for the data that's returned (a queue is the way I usually handle it...).

    Here is an example (recognizing that this may not do what you want - plot all the figures "simultaneously"? - plt.show() blocks so only one is drawn at a time and I note that you do not have it in your sample code - but without I don't see anything on my screen - however, if I take it out - there is no blocking and no error because all GUI functions are happening in the main thread):

    import multiprocessing
    import matplotlib.pyplot as plt
    import numpy as np
    import rpy2.robjects as robjects
    
    data_queue = multiprocessing.Queue()
    
    
    def main():
        pool = multiprocessing.Pool()
        num_figs = 10
    
        # generate some random numbers
        input = zip(np.random.randint(10,10000,num_figs), range(num_figs))  
        pool.map(worker, input)
    
        figs_complete = 0
        while figs_complete < num_figs:
            data = data_queue.get()
            plt.figure()
            plt.plot(data)
            plt.show()
            figs_complete += 1
    
    def worker(args):
        num, i = args
        data = np.random.randn(num).cumsum()
        data_queue.put(data)
        print('done ',i)
    
    main()
    

    Hope this helps.

提交回复
热议问题