[PyQt] PyQt crashed for a real time application

jian li jnli36587 at gmail.com
Thu Oct 28 15:16:56 BST 2010


Guys, I write a programming to capture stocks price 4 time a secs and show
it on a QTableWidget. But it crashed some time after running for about 3
hours.I created a thread to get data, and each QThread for each
QTableWIdget(each stock basically). have a feeling it is related the the
Qthread or Thread, but don't know how to find the bug.

Below is part of the code and backtrace, thanks very much in advance

class MyThread(QThread):
    def __init__(self, input, parent = None):
        QThread.__init__(self, parent)
        self.alive = 0
        self.running = 0
        self.n = 0
        self.widg = parent

    def run(self):
        global thread_idx
        thread_idx += 1

    def toggle(self):
        if self.running:
            self.running = 0
        else:
            self.running = 1

    def stop(self):
        self.alive = 0

class MainWindow(QMainWindow):

    def __init__(self, win_parent = None):
        global local_flag
        QMainWindow.__init__(self, win_parent)
        self.create_widgets()
        self.subtable = {}
        self.setGeometry(control_Geom[0], control_Geom[1], control_Geom[2],
control_Geom[3] )

    def create_widgets(self):
        self.label = QLabel("Stock:")
        self.main_edit = QLineEdit()
        self.main_button = QPushButton("Go")
        QObject.connect(self.main_button
                , SIGNAL("clicked()")
                , self.on_main_clicked1)
        self.open_file = QPushButton("Open File")
        QObject.connect(self.open_file
                , SIGNAL("clicked()")
                , self.on_open_file)
        QObject.connect(self.main_edit
                , SIGNAL("returnPressed()")
                , self.on_main_clicked1)
        h_box = QHBoxLayout()
        h_box.addWidget(self.label)
        h_box.addWidget(self.main_edit)
        h_box.addWidget(self.main_button)
        h_box.addWidget(self.open_file)
        central_widget = QWidget()
        central_widget.setLayout(h_box)
        self.setCentralWidget(central_widget)

    def on_open_file(self):
        thread.start_new_thread(run_update_local, (self.subtable,))

    def on_main_clicked1(self):
        global last_time, num_prices
        symbol = self.main_edit.displayText()
        symbol = str(symbol).upper()
        popTable = MyWindow(symbol)
        self.subtable[symbol] = popTable
        last_time[symbol] = 0.
        popTable.show()
        popTable.thread = MyThread(symbol, popTable.tb)
        popTable.thread.toggle()
        popTable.thread.start()
    def closeEvent(self, event):
            global port, local_flag
            for s in self.subtable:
                 self.subtable[s].close()
                 self.subtable[s].tb.close()
            if not local_flag:
                ps_proc = os.popen("ssh %s ps -ef | grep %s |awk '{ print $2
} '"%(server_get_feed, port), "r")
                s_proc = os.popen("ps -ef | grep %s | grep ssh | awk '{
print $2 } '"%port, "r")
                for sp in s_proc:
                    os.system("kill -9 %d"%int(sp))
                for sp in ps_proc:
                    os.system("ssh %s kill -9 %d"%(server_get_feed,
int(sp)))
                l_proc = os.popen("ps -ef | grep %s | awk '{ print $2 }
'"%port, "r")
                for sp in l_proc:
                    os.system("kill -9 %d"%int(sp))

class MyWindow(QWidget):
    def __init__(self, symbol, *args):
        global num_prices
        QWidget.__init__(self, *args)
        self.tb = MyTable(symbol, num_prices, 5)
        self.timer = QPushButton("Time: 00:00:00.000000", self)
        self.re_center = QPushButton("ReCenter", self)
        self.clear = QPushButton("Clear", self)

        layout = QVBoxLayout()
        layout.addWidget(self.tb)
        layout.addWidget(self.timer)
        layout.addWidget(self.re_center)
        layout.addWidget(self.clear)
        self.setLayout(layout)
        self.setGeometry(table_Geom[0], table_Geom[1], table_Geom[2],
table_Geom[3])
        self.setWindowTitle("%s"%symbol)


        QWidget.connect(self.clear, SIGNAL("clicked()"),
self.tb.clear_quotes)
        QWidget.connect(self.re_center, SIGNAL("clicked()"),
self.tb.recenter)

class MyTable(QTableWidget):
    def __init__(self, symbol, *args):
        self.symbol = symbol
        QTableWidget.__init__(self, *args)
        self.setHorizontalHeaderLabels("     ")
        self.verticalHeader().hide()
        self.setmydata()
        font = QFont("Arial", 14)
        font.setBold(True)
        self.setFont(font)
       # self.setGeometry(300,100,355,750)
        self.setAutoScroll(True)

        self.setColumnWidth(0,40)
        self.setColumnWidth(1,70)
        self.setColumnWidth(2,60)
        self.setColumnWidth(3,70)
        self.setColumnWidth(4,40)

        self.buy_trades = {}
        self.sell_trades = {}
        self.t = threading.Timer(20, self.recenter)
        self.t.start()
        self.dataset = {}
        self.last_bs_data = []
        self.best_buy = 0.
        self.best_sell = 0.
        self.pricelist = []
        self.last_pricelist = []
        self.drawdata = []
        self.drawprice = []
        self.buy_quotes = {}
        self.sell_quotes = {}
        #print >>sys.stderr, "reached end"

    def closeEvent(self, event):
        self.t.cancel()

    def set_newrow(self, n_idx):
        self.insertRow(n_idx)
        newitem = QTableWidgetItem(n_idx)
        newitem.setSizeHint(QSize(100,32))
        newitem.setBackground(QBrush(QColor(153,153,153)))
        self.setItem(n_idx, 2, newitem)
        newitem = QTableWidgetItem(n_idx)
        newitem.setSizeHint(QSize(100,32))
        newitem.setBackground(QBrush(QColor(204,204,195)))
        self.setItem(n_idx, 0, newitem)
        newitem = QTableWidgetItem(n_idx)
        newitem.setSizeHint(QSize(100,32))
        newitem.setBackground(QBrush(QColor(204,204,195)))
        self.setItem(n_idx, 4, newitem)
        newitem = QTableWidgetItem(n_idx)
        newitem.setSizeHint(QSize(100,32))
        newitem.setBackground(QBrush(QColor(0,51,153)))
        self.setItem(n_idx, 1, newitem)
        newitem = QTableWidgetItem(n_idx)
        newitem.setSizeHint(QSize(100,32))
        newitem.setBackground(QBrush(QColor(153,0,0)))
        self.setItem(n_idx, 3, newitem)

    def setmydata(self):
        global num_prices
        n = 0
        for n in xrange(5):
            if n == 2:
                for m in xrange(num_prices):
                    newitem = QTableWidgetItem(m)
                    newitem.setSizeHint(QSize(100,32))
                    newitem.setBackground(QBrush(QColor(153,153,153)))
                    self.setItem(m, n, newitem)
            elif n == 3:
                for m in xrange(num_prices):
                    newitem = QTableWidgetItem(m)
                    newitem.setText(" ")
                    newitem.setBackground(QBrush(QColor(153,0,0)))
                    self.setItem(m, n, newitem)
            elif n == 1:
                for m in xrange(num_prices):
                    newitem = QTableWidgetItem(m)
                    newitem = QTableWidgetItem(m)
                    newitem.setText(" ")
                    newitem.setBackground(QBrush(QColor(0,51,153)))
                    self.setItem(m, n, newitem)
            else:
                for m in xrange(num_prices):
                    newitem = QTableWidgetItem(m)
                    newitem = QTableWidgetItem(m)
                    newitem.setText(" ")
                    newitem.setBackground(QBrush(QColor(204,204,195)))
                    self.setItem(m, n, newitem)

    def draw_new(self):
        lastlen = len(self.last_pricelist)
        newlen = len(self.pricelist)
        if newlen > lastlen and newlen > 20:
            if lastlen > 20:
                for i in xrange(newlen - lastlen):
                    self.set_newrow(lastlen + i)
            else:
                for i in xrange(newlen - 20):
                    self.set_newrow(20 + i)
        elif lastlen > newlen:
            if lastlen < 20:
                for i in xrange(20 - newlen):
                    self.clearline(newlen + i)
            else:
                if newlen < 20 or newlen == 20:
                    for i in xrange(lastlen - 20):
                        self.removeRow(20)
                    for i in xrange(20 - newlen):
                        self.clearline(newlen + i)
                else:
                    for i in xrange(lastlen - newlen):
                        self.removeRow(newlen)

        for i in xrange(len(self.drawprice)):
            if self.drawprice[i] != 0:
                self.item(i, 2).setText("%.02f"%self.drawprice[i])
                self.item(i, 2).setForeground(QBrush(QColor(255,255,255)))
        for i in xrange(len(self.drawdata)):
            for j in xrange(4):
                if j == 0:
                    if self.drawdata[i][j] == -1:
                        self.item(i, 1).setText(" ")
                        self.item(i,
1).setBackground(QBrush(QColor(0,51,153)))
                    elif self.drawdata[i][j] != 0:
                        self.item(i, 1).setText("%d"%self.drawdata[i][j])
                        self.item(i,
1).setForeground(QBrush(QColor(255,255,255)))
                        self.item(i,
1).setBackground(QBrush(QColor(20,80,153)))
                if j == 1:
                    if self.drawdata[i][j] == -1:
                        self.item(i, 3).setText(" ")
                        self.item(i,
3).setBackground(QBrush(QColor(153,0,0)))
                    elif self.drawdata[i][j] != 0:
                        self.item(i, 3).setText("%d"%self.drawdata[i][j])
                        self.item(i,
3).setForeground(QBrush(QColor(255,255,255)))
                        self.item(i,
3).setBackground(QBrush(QColor(204,0,0)))
                if j == 2:
                    if self.drawdata[i][j] == -1:
                        self.item(i, 0).setText(" ")
                        self.item(i,
0).setBackground(QBrush(QColor(204,204,195)))
                        self.item(i,
2).setBackground(QBrush(QColor(153,153,153)))
                    elif self.drawdata[i][j] != 0:
                        self.item(i, 0).setText("%d"%self.drawdata[i][j])
                        self.item(i,
0).setBackground(QBrush(QColor(0,255,0)))
                        self.item(i,
2).setBackground(QBrush(QColor(0,51,153)))
                if j == 3:
                    if self.drawdata[i][j] == -1:
                        self.item(i, 4).setText(" ")
                        self.item(i,
4).setBackground(QBrush(QColor(204,204,195)))
                        self.item(i,
2).setBackground(QBrush(QColor(153,153,153)))
                    elif self.drawdata[i][j] != 0:
                        self.item(i, 4).setText("%d"%self.drawdata[i][j])
                        self.item(i,
4).setBackground(QBrush(QColor(0,255,0)))
                        self.item(i,
2).setBackground(QBrush(QColor(204,0,0)))
        time.sleep(0.0000001)
      #  print self.drawdata
      #  print self.drawprice

    def recenter(self):
          print >> GUI_TEST_LOG, "LINE %d recentering"%lineno(),
self.best_buy
          try:
              index = self.price_idx[self.best_buy] +
len(self.high_outliers)
              self.scrollToItem(self.item(index, int(2)),
QAbstractItemView.PositionAtCenter)
          except:
              pass
          t = threading.Timer(20, self.recenter).start()


*#0  0x00002b481c6b2f93 in QVector<QLayoutStruct>::realloc
(this=0x7fffcff2f990, asize=1398090296, aalloc=0)
    at ../../include/QtCore/qvector.h:502
#1  0x00002b481c6d3c33 in qGeomCalc (chain=..., start=0, count=4, pos=11,
space=737, spacer=-1) at ../../include/QtCore/qvector.h:134
#2  0x00002b481c6b05c8 in QBoxLayout::setGeometry (this=0x16470dd0, r=...)
at kernel/qboxlayout.cpp:850
#3  0x00002b481c05230f in ?? () from
/usr/local/lib/python2.7/site-packages/PyQt4/QtGui.so
#4  0x00002b481c6d16da in QLayoutPrivate::doResize (this=0x16470e20, r=...)
at kernel/qlayout.cpp:681
#5  0x00002b481c6d1fcf in QLayout::activate (this=0x16470dd0) at
kernel/qlayout.cpp:1259
#6  0x00002b481c6a00f2 in QApplicationPrivate::notify_helper
(this=0x160952a0, receiver=0x163075e0, e=0x2aaaac00a3c0)
    at kernel/qapplication.cpp:4293
#7  0x00002b481c6a9ca2 in QApplication::notify (this=0x16094750,
receiver=0x163075e0, e=0x2aaaac00a3c0) at kernel/qapplication.cpp:3736
#8  0x00002b481c063144 in ?? () from
/usr/local/lib/python2.7/site-packages/PyQt4/QtGui.so
#9  0x00002b481b6407e4 in QCoreApplication::notifyInternal (this=0x16094750,
receiver=0x163075e0, event=0x2aaaac00a3c0)
    at kernel/qcoreapplication.cpp:726
#10 0x00002b481b64154d in QCoreApplicationPrivate::sendPostedEvents
(receiver=0x0, event_type=0, data=0x160885c0)
    at ../../include/QtCore/qcoreapplication.h:215
#11 0x00002b481b66ab83 in sendPostedEvents (s=<value optimized out>) at
../../include/QtCore/qcoreapplication.h:220
#12 postEventSourceDispatch (s=<value optimized out>) at
kernel/qeventdispatcher_glib.cpp:276
#13 0x0000003d55a2cdb4 in g_main_context_dispatch () from
/lib64/libglib-2.0.so.0
#14 0x0000003d55a2fc0d in ?? () from /lib64/libglib-2.0.so.0
#15 0x0000003d55a3011e in g_main_context_iteration () from
/lib64/libglib-2.0.so.0
#16 0x00002b481b66ade5 in QEventDispatcherGlib::processEvents
(this=0x16101320, flags=...) at kernel/qeventdispatcher_glib.cpp:412
#17 0x00002b481c74a94f in QGuiEventDispatcherGlib::processEvents (this=0x0,
flags=<value optimized out>)
    at kernel/qguieventdispatcher_glib.cpp:204
#18 0x00002b481b63f975 in QEventLoop::processEvents (this=<value optimized
out>, flags=...) at kernel/qeventloop.cpp:149
#19 0x00002b481b63fccd in QEventLoop::exec (this=0x7fffcff30350, flags=...)
at kernel/qeventloop.cpp:201
#20 0x00002b481b6418e4 in QCoreApplication::exec () at
kernel/qcoreapplication.cpp:1003
#21 0x00002b481c063e3d in ?? () from
/usr/local/lib/python2.7/site-packages/PyQt4/QtGui.so
#22 0x000000000049b574 in call_function (f=0x15c91170, throwflag=<value
optimized out>) at Python/ceval.c:4012
#23 PyEval_EvalFrameEx (f=0x15c91170, throwflag=<value optimized out>) at
Python/ceval.c:2665
#24 0x000000000049d3bb in PyEval_EvalCodeEx (co=0x2b48178c7a30,
globals=<value optimized out>, locals=<value optimized out>, args=0x0,
    argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at
Python/ceval.c:3252
#25 0x000000000049d432 in PyEval_EvalCode (co=0x0, globals=0x0,
locals=0x164b8840) at Python/ceval.c:666
#26 0x00000000004bf321 in run_mod (fp=0x15c6b300, filename=0x7fffcff31bdd
"/linden/bin/gui_depthbook.py", start=<value optimized out>,
    globals=0x15be9f50, locals=0x15be9f50, closeit=1, flags=0x7fffcff30800)
at Python/pythonrun.c:1346
#27 PyRun_FileExFlags (fp=0x15c6b300, filename=0x7fffcff31bdd
"/linden/bin/gui_depthbook.py", start=<value optimized out>,
globals=0x15be9f50,
    locals=0x15be9f50, closeit=1, flags=0x7fffcff30800) at
Python/pythonrun.c:1332
#28 0x00000000004bf5d8 in PyRun_SimpleFileExFlags (fp=<value optimized out>,
filename=0x7fffcff31bdd "gui_depthbook.py",
    closeit=1, flags=0x7fffcff30800) at Python/pythonrun.c:936
#29 0x00000000004148cc in Py_Main (argc=<value optimized out>, argv=<value
optimized out>) at Modules/main.c:599
#30 0x0000003d5321d994 in __libc_start_main () from /lib64/libc.so.6
#31 0x0000000000413b19 in _start ()*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101028/3e4cc154/attachment-0001.html>


More information about the PyQt mailing list