|
多线程绘图的时候发生错误,跪求大神指点!
-------------------------------以下是代码--------------------------------------
from threading import Thread,Lock
import time
import datetime
import matplotlib.pyplot as plt
import random
lock = Lock()
def run(i):
for j in range(2):
print('{}-{}开始'.format(i,j))
a = datetime.datetime.now()
lock.acquire()
time.sleep(1)
draw_day(i,j)
lock.release()
b = datetime.datetime.now()
end = '{}-{}结束,{},本次共运行了{}秒!'.format(i,j,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),(b - a).seconds)
print(end)
def draw_day(i,j):
x1 = [1,2,9]
y1 = [1,8,20]
x2 = [2,3,12]
y2 = [8,40,18]
plt.figure(figsize=(10,6))
plt.plot(x1,y1)
plt.plot(x2,y2)
#plt.show()
plt.savefig('{}-{}.png'.format(i,j))
plt.close()
def Thread_w():
Threads = []
for k in range(3):
t = Thread(target=run,args=(k,),name=k)
Threads.append(t)
for t in Threads:
t.start()
for t in Threads:
t.join()
print('='*50)
while True:
a = datetime.datetime.now()
Thread_w()
b = datetime.datetime.now()
end = '{},本次共运行了{}秒!'.format(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),(b - a).seconds)
print(end)
print('='*50)
----------------------以下是报错内容-----------------------------
==================================================
0-0开始
1-0开始
2-0开始
0-0结束,2018-01-21 11:27:11,本次共运行了1秒!
0-1开始
1-0结束,2018-01-21 11:27:12,本次共运行了2秒!
1-1开始
2-0结束,2018-01-21 11:27:13,本次共运行了3秒!
2-1开始
0-1结束,2018-01-21 11:27:14,本次共运行了3秒!
1-1结束,2018-01-21 11:27:16,本次共运行了3秒!
2-1结束,2018-01-21 11:27:17,本次共运行了3秒!
2018-01-21 11:27:17,本次共运行了7秒!
==================================================
0-0开始
1-0开始
2-0开始
0-0结束,2018-01-21 11:27:19,本次共运行了1秒!
0-1开始
1-0结束,2018-01-21 11:27:20,本次共运行了2秒!
1-1开始
2-0结束,2018-01-21 11:27:21,本次共运行了3秒!
2-1开始
0-1结束,2018-01-21 11:27:22,本次共运行了3秒!
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8ABF86518>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8ABF86B00>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8BAE23668>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8BABAE278>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AEBAAFD0>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AEE57400>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AF12CF60>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8AF842320>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Exception ignored in: <bound method Image.__del__ of <tkinter.PhotoImage object
at 0x000000D8B297F3C8>>
Traceback (most recent call last):
File "C:\Python36\lib\tkinter\__init__.py", line 3501, in __del__
self.tk.call('image', 'delete', self.name)
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread
|
|