简体   繁体   English

Matplotlib 和 :RuntimeError: 主线程不在主循环中:

[英]Matplotlib and :RuntimeError: main thread is not in main loop:

I'm trying to run multiple repetitive tasks in parallel under python.我正在尝试在 python 下并行运行多个重复性任务。 I'm pretty new to multiprocessing but as all the tasks are independant, I used the simple following piece of code :我对多处理很陌生,但由于所有任务都是独立的,我使用了以下简单的代码:

    import numpy as np
    import sys
    import os
    import glob
    import matplotlib.pyplot as plt
    import concurrent.futures as Cfut

    def analize(simul, N_thread):
        path = os.getcwd()

        print('Analizing topo ...')
        Data = output of some calculations

        print('Writing Data ...')
        np.save('Data', Data)

        print('Data saved')
        print('Printing figures')

        plt.figure()
        plt.plot(Data[0])
        plt.savefig('figure1.pdf)

        plt.clf()
        plt.plot(Data[0])
        plt.savefig('figure1.pdf)

        plt.close('all')
        print('figures saved')
        os.chdir(path

if __name__ == '__main__':
    list_simul = sorted(glob.glob('Layer*'))
    executor = Cfut.ProcessPoolExecutor(5)
   #executor = Cfut.ThreadPoolExecutor(N_jobs)
    futures = [executor.submit(analize, item, 10) for item in list_simul]
    Cfut.wait(futures)

It ran very well during 3 months, and since since morning I get sometimes the following error :它在 3 个月内运行良好,从早上开始我有时会收到以下错误:

Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object at 0x7fac6c187f98>>
    Traceback (most recent call last):
      File "/usr/lib/python3.5/tkinter/__init__.py", line 3359, in __del__
        self.tk.call('image', 'delete', self.name)
    RuntimeError: main thread is not in main loop

What is odd is that some jobs are done without any problem, and it happens not all the time.奇怪的是,有些工作可以毫无问题地完成,而且并非总是如此。 With a bit of research, I found a lot of posts with this problem, all of them were related to GUI.通过一些研究,我发现了很多关于这个问题的帖子,它们都与 GUI 相关。 I understood the problem, but I guess I should be able to find a workaround since I'm not showing any figure, just creating the object and saving it, and since all the tasks are independent.我理解这个问题,但我想我应该能够找到一种解决方法,因为我没有显示任何图形,只是创建对象并保存它,并且因为所有任务都是独立的。

Any hint ?任何提示?

As seen here , have you tried adding the following code to the top of your imports?正如所看到这里,你有没有尝试添加以下代码到您的进口顶部?

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

matplotlib uses TkAgg for default. matplotlib 默认使用TkAgg Backends like TkAgg , FltkAgg , GTK , GTKAgg , GTKCairo , Wx , and WxAgg are all GUI based.TkAggFltkAggGTKGTKAggGTKCairoWxWxAgg都是基于 GUI 的。 And most GUI backends require being run from the main thread.并且大多数 GUI 后端需要从主线程运行。 Thus, if you are running on an environment without GUI, it will throw RuntimeError: main thread is not in main loop .因此,如果您在没有 GUI 的环境中运行,它将抛出RuntimeError: main thread is not in main loop

Therefore, just switch to backends that do not use GUI: Agg , Cairo , PS , PDF , or SVG .因此,只需切换到不使用 GUI 的后端: AggCairoPSPDFSVG

For example:例如:

import matplotlib.pyplot as plt
plt.switch_backend('agg')

References: https://matplotlib.org/stable/faq/howto_faq.html#work-with-threads参考资料: https : //matplotlib.org/stable/faq/howto_faq.html#work-with-threads

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时错误:主线程不在主循环中使用带有 Django 的 Matplotlib - RuntimeError: main thread is not in main loop using Matplotlib with Django RuntimeError:主线程不在主循环中 - RuntimeError: main thread is not in main loop (Python tkinter):RuntimeError:主线程不在主循环中 - (Python tkinter): RuntimeError: main thread is not in main loop PySimpleGUI。 运行时错误:主线程不在主循环中 - PySimpleGUI. RuntimeError: main thread is not in main loop kochat in use RuntimeError: 主线程不在主循环中 - kochat in use RuntimeError: main thread is not in main loop 使用 matplotlib 绘制图形时出现间歇性错误“RuntimeError: main thread is not in main loop” - Intermittent error while plotting graph using matplotlib “RuntimeError: main thread is not in main loop” RuntimeError:主线程不在主循环中-绘制两个图 - RuntimeError: main thread is not in main loop-Plotting in two figures RuntimeError:使用tkinter.simpledialog时主线程不在主循环中 - RuntimeError: main thread is not in main loop while using tkinter.simpledialog 调试 Tkinter 代码给出“RuntimeError:主线程不在主循环中” - Debugging Tkinter code gives "RuntimeError: main thread is not in main loop" RuntimeError:在 Timer 上设置 y 坐标时,主线程不在主循环中 - RuntimeError: main thread is not in main loop when setting y coordinate on Timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM