i want to accomplish a multithread dynamic demo.When the program starts to run, it shows the plot and everything is OK.But it obvious runs unnormally,the mouse pointer becomes a spinning circle. Then the program crashs and exits with the error codes in the tltle.The complete codes are as follows
#-*-coding:utf-8-*-
import matplotlib
from matplotlib.patches import Circle
import dask
import matplotlib.pyplot as plt
import xlrd
import numpy as np
from matplotlib.animation import FuncAnimation
import matplotlib.ticker as mticker
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import time
from matplotlib.offsetbox import AnnotationBbox,OffsetImage
from PIL import Image
import random
from time import ctime,sleep
import threading
#matplotlib.use('Agg')
#地图可视化
fig=plt.figure(figsize=(20,10))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines()
ax.stock_img()
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
linewidth=2, color='gray', alpha=15, linestyle='--')
gl.xlabels_top = False
gl.ylabels_left = False
gl.xlines = False
gl.xlocator = mticker.FixedLocator([-180, -45, 0, 45, 180])
gl.xformatter = LONGITUDE_FORMATTER
gl.yformatter = LATITUDE_FORMATTER
gl.xlabel_style = {'size': 15, 'color': 'gray'}
gl.xlabel_style = {'color': 'red', 'weight': 'bold'}
img=Image.open(r'E:\python_file\untitled\p.png')
imagebox=OffsetImage(img,zoom=0.05)
imagebox.image.axes=ax
ab=AnnotationBbox(imagebox,[55,10],pad=0,frameon=False)
ax.add_artist(ab)
ac=AnnotationBbox(imagebox,[63,0],pad=0,frameon=False)
ax.add_artist(ac)
ad=AnnotationBbox(imagebox,[70,-10],pad=0,frameon=False)
ax.add_artist(ad)
#============================================#攻击
tolerance=1
x_m1,y_m1=random.randint(-180,180),random.randint(-90,90)
v_m1=170
x_m2,y_m2=random.randint(-180,180),random.randint(-90,90)
v_m2=v_m1
x_m3,y_m3=random.randint(-180,180),random.randint(-90,90)
v_m3=v_m1
x_m4,y_m4=55,10
x_m5,y_m5=63,0
x_m6,y_m6=70,-10
class target():
"""docstring for target"""
def __init__(self, x, y):
self.x = x
self.y = y
target1=target(x_m4,y_m4)
target2=target(x_m5,y_m5)
target3=target(x_m6,y_m6)
v=v_m1
class missile(threading.Thread):
"""docstring for missile"""
def __init__(self, x, y,name):
super(missile,self).__init__()
self.x = x
self.y = y
self.name=name
def forward(self, v, target1):
"""docstring for forward"""
if self.x < target1.x:
alpha = np.arctan((target1.y - self.y) / (target1.x - self.x))
elif self.x > target1.x:
alpha = np.pi + np.arctan((target1.y - self.y) / (target1.x - self.x))
elif self.x == target1.x and self.y < target1.y:
alpha = np.pi / 2
else:
alpha = -np.pi / 2
self.x = self.x + v * 0.01 * np.cos(alpha)
self.y = self.y + v * 0.01 * np.sin(alpha)
return self.x, self.y
def distance(self, target1):
"""docstring for distance"""
return np.sqrt((self.x - target1.x) ** 2 + (self.y - target1.y) ** 2)
def run(self):
while True:
if self.distance(target1) < tolerance or self.distance(target2) < tolerance or self.distance(
target3) < tolerance:
print ("collision")
break
if self.distance(target1) < self.distance(target2) and self.distance(target1) < self.distance(target3):
self.x, self.y = self.forward(v, target1)
if self.distance(target2) < self.distance(target1) and self.distance(target2) < self.distance(target3):
self.x, self.y = self.forward(v, target2)
if self.distance(target3) < self.distance(target2) and self.distance(target3) < self.distance(target1):
self.x, self.y = self.forward(v, target3)
plt.plot(self.x, self.y, 'o')
fig.canvas.draw()
fig.canvas.flush_events()
m2=missile(x_m2,y_m2,'mm')
m1=missile(x_m1,y_m1,'mn')
m3=missile(x_m3,y_m3,'md')
print "m1前"
m1.start()
print "m1后"
m2.start()
print "m2后"
m3.start()
print "m3后"
plt.show()
Could you have some suggestions to solve the crash to make the program runs normally? In my opinion,the problem is related to multithreading
finally,i find there are resource accountings in the multithreadings.putting a lock on the plot process solves the problem