将matplotlib窗口从子进程中分离
3 个回答
3
我建议使用 os.fork() 作为最简单的解决方案。它确实是守护进程中使用的技巧,但不需要两个脚本,而且非常简单。比如:
import os
proc_num = os.fork()
if proc_num != 0:
#This is the parent process, that should quit immediately to return to the
#shell.
print "You can kill the graph with the command \"kill %d\"." % proc_num
import sys
sys.exit()
#If we've made it to here, we're the child process, that doesn't have to quit.
import matplotlib.pyplot as plt
plt.plot([1,2,3],[4,5,6])
plt.show()
6
刚刚发现了plt.show()中的一个参数。把block设置为False的话,会弹出图形窗口,接着继续执行后面的代码,并且在脚本运行结束后还会停留在解释器里(前提是你是在交互模式下运行的 -i)。
plt.show(block=False)
3
这个在Unix系统上可以用:
import pylab
import numpy as np
import multiprocessing as mp
import os
def display():
os.setsid()
pylab.show()
mu, sigma = 2, 0.5
v = np.random.normal(mu,sigma,10000)
(n, bins) = np.histogram(v, bins=50, normed=True)
pylab.plot(bins[:-1], n)
p=mp.Process(target=display)
p.start()
当你在终端运行这个脚本时,会显示出pylab的图形。按下Ctrl-C可以结束主脚本,但图形会继续显示。