【发布时间】:2019-03-21 05:46:14
【问题描述】:
我正在尝试在 python 下并行运行多个重复任务。我对多处理很陌生,但由于所有任务都是独立的,我使用了以下简单的代码:
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)
它在 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
奇怪的是,有些工作完成时没有任何问题,而且并非总是如此。经过一番研究,我发现了很多关于这个问题的帖子,它们都与GUI有关。我理解这个问题,但我想我应该能够找到解决方法,因为我没有显示任何数字,只是创建对象并保存它,而且所有任务都是独立的。
有什么提示吗?
【问题讨论】:
标签: python multithreading matplotlib tkinter multiprocessing