我只是想在調試時使用matplotlib來可視化一些數據。按照這個頁面:Analyzing C/C++ matrix in the gdb debugger with Python and Numpy - CodeProject,它工作正常,除非matplotlib GUI只是阻止GDB的命令行。這意味着如果我打開GUI窗口,GDB的命令行被凍結,並且在關閉pyplot窗口之前,我不能在GDB命令中輸入任何內容。用於GDB python漂亮打印機的非阻塞pyplot GUI
爲了解決這個問題,我只是試圖在另一個線程運行的繪圖代碼,以簡化測試情況下,我只需要創建下面
import numpy as np
from matplotlib import pyplot as plt
from threading import Thread
class MyThread (Thread):
def __init__(self, thread_id):
Thread.__init__(self)
self.thread_id = thread_id
def run(self):
x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)
plt.show(block = True) #this cause the mainloop
thread1 = MyThread(1)
thread1.start()
名爲「test-pyplot.py」與內容的簡單Python源代碼
現在,在GDB命令行下,我只需鍵入:source test-pyplot.py
,並打開一個非阻塞GUI,它看起來不錯,GDB的命令行仍然可以接受命令,到目前爲止這麼好。
但是,當我關閉繪圖窗口,然後再次運行source test-pyplot.py
時,問題發生,這次GDB掛起。
我在Windows下使用python 2.7.6,我發現matplotlib默認使用tkAgg
作爲繪圖後端,所以我試着看看這是否會發生在正常的tk GUI窗口中。這是一個名爲「test-tk.py」另一個測試Python文件,其中有如下內容:
from Tkinter import *
from threading import Thread
class App():
def __init__(self):
self.g=Tk()
self.th=Thread(target=self.g.mainloop)
self.th.start()
def destroy(self):
self.g.destroy()
a1 = App()
如果我跑下GDB提示命令source test-tk.py
,一個TK窗口會顯示出來,GDB還活着(不凍結),我可以關閉tk窗口,並再次鍵入命令source test-tk.py
,並且每件事情都很好,GDB不會掛起。我甚至可以在不關閉第一個tk窗口的情況下運行命令source test-tk.py
兩次,然後會顯示兩個tk窗口。
問題:如何在非阻塞模式下正確顯示matplotlib pyplot數字,這不會掛起GDB?謝謝。 通常,plt.show
將在內部調用Tkinter包的一個事件循環的mainloop
函數。 matplotlib確實有一個名爲交互模式的選項,可以通過調用`plt.ion()'來啓用它,但它不能解決我的問題。
嗨,湯姆,感謝您的答覆。搜索gdb bugzilla,我發現一個:[gdb在用matplotlib繪圖後掛起](https://sourceware.org/bugzilla/show_bug.cgi?id=14382),那裏你還提到了SIGCHLD。我瀏覽了一下gdb-gui的源代碼,你創建了一個.so來修復這些類型的信號問題,理解這個黑客對我來說有點難,因爲它看起來像只適用於非Windows系統。順便說一句,你建議在一個單獨的線程中運行GUI,是的,我在一個單獨的線程中運行我的python劇情代碼,並且所有的main-loop函數都在單獨的線程中運行。 – ollydbg23
它看起來像Ipython確實解決了這個問題,看到這個帖子:https://sourceforge.net/p/matplotlib/mailman/message/9333243/和[用matplotlib繪圖](http://heim.ifi.uio。no/inf3330/scripting/doc/python/ipython/node14.html),另外我看到Wing IDE也有這個固定的,請看:http://wingware.com/doc/howtos/matplotlib – ollydbg23
嗨,湯姆,你是對的,在我的代碼中,GUI是在主線程中創建的,並且gui事件循環在單獨的線程中運行。但是,如果我在一個單獨的線程中更改所有代碼,就像您在[Cliffs of Inanity> 10. Wacky stuff - tromey.com]中創建的頁面一樣(http://tromey.com/blog/?p=550) ,我仍然有這個問題。我發現許多shell都有「集成的GUI事件循環」,請參閱:[IPython GUI Support Notes - IPython 3.0.0-dev documentation](http://ipython.readthedocs.org/en/latest/development/inputhook_app。 html) – ollydbg23