我的建议是使用 python多处理模块而不是线程模块。我已经能够对您的示例代码进行轻微修改,并成功地将 matplotlib 绘图卸载到子进程,同时主进程中的控制流继续(参见下面的代码)。
如果您希望子进程在更大的代码控制流的上下文中与父进程来回通信(这不是'您的问题中没有完全描述)。请注意,多处理具有绕过 python全局解释器锁和允许您利用多核计算机体系结构的额外优势。
#a slight modification of your code using multiprocessing
import matplotlib
matplotlib.use("qt4agg")
import matplotlib.pyplot as plt
#import threading
#let's try using multiprocessing instead of threading module:
import multiprocessing
import time
#we'll keep the same plotting function, except for one change--I'm going to use the multiprocessing module to report the plotting of the graph from the child process (on another core):
def plot_a_graph():
f,a = plt.subplots(1)
line = plt.plot(range(10))
print multiprocessing.current_process().name,"starting plot show process" #print statement preceded by true process name
plt.show() #I think the code in the child will stop here until the graph is closed
print multiprocessing.current_process().name,"plotted graph" #print statement preceded by true process name
time.sleep(4)
#use the multiprocessing module to perform the plotting activity in another process (i.e., on another core):
job_for_another_core = multiprocessing.Process(target=plot_a_graph,args=())
job_for_another_core.start()
#the follow print statement will also be modified to demonstrate that it comes from the parent process, and should happen without substantial delay as another process performs the plotting operation:
print multiprocessing.current_process().name, "The main process is continuing while another process deals with plotting."