擅长:python、mysql、java
<p>如果您使用的库支持以非阻塞方式使用,则最好始终与它联系。</p>
<p>但是,如果您想要一个更通用的解决方案,或者如果没有其他方法,您可以使用python中包含的<a href="http://docs.python.org/library/multiprocessing.html" rel="noreferrer">^{<cd1>}</a>模块在一个分离的进程中运行任何阻塞。计算将继续:</p>
<pre><code>from multiprocessing import Process
from matplotlib.pyplot import plot, show
def plot_graph(*args):
for data in args:
plot(data)
show()
p = Process(target=plot_graph, args=([1, 2, 3],))
p.start()
print 'yay'
print 'computation continues...'
print 'that rocks.'
print 'Now lets wait for the graph be closed to continue...:'
p.join()
</code></pre>
<p>这有启动一个新进程的开销,有时在复杂的场景中很难调试,所以我更喜欢另一个解决方案(使用<code>matplotlib</code>的<a href="https://stackoverflow.com/questions/458209/is-there-a-way-to-detach-matplotlib-plots-so-that-the-computation-can-continue#458295">nonblocking API calls</a>)</p>