Python rpy2 and matplotlib conflict when using multiprocessing

前端 未结 5 1818
误落风尘
误落风尘 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 15:17

    I had a similar issue with my worker, which was loading some data, generating a plot, and saving it to a file. Note that this is slightly different than what the OP's case, which seems to be oriented around interactive plotting. Still, I think it's relevant.

    A simplified version of my code:

    def worker(id):
        data = load_data(id)
        plot_data_to_file(data) # Generates a plot and saves it to a file.
    
    def plot_something_parallel(ids):
        pool = multiprocessing.Pool()
        pool.map(worker, ids)
    
    plot_something_parallel(ids=[1,2,3])
    

    This caused the same error others mention:

    The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
    Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
    

    Following @bbbruce's train of thought, I solved my problem by switching the matplotlib backend from TKAgg to the default. Specifically, I commented out the following line in my matplotlibrc file:

    #backend : TkAgg
    

提交回复
热议问题