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.<br>
<br>Below is part of the code and backtrace, thanks very much in advance<br><br>class MyThread(QThread):<br> def __init__(self, input, parent = None):<br> QThread.__init__(self, parent)<br> self.alive = 0<br>
self.running = 0<br> self.n = 0<br> self.widg = parent<br><br> def run(self):<br> global thread_idx<br> thread_idx += 1<br> <br> def toggle(self):<br> if self.running:<br>
self.running = 0<br> else:<br> self.running = 1<br><br> def stop(self):<br> self.alive = 0<br><br>class MainWindow(QMainWindow):<br><br> def __init__(self, win_parent = None):<br>
global local_flag<br> QMainWindow.__init__(self, win_parent)<br> self.create_widgets()<br> self.subtable = {}<br> self.setGeometry(control_Geom[0], control_Geom[1], control_Geom[2], control_Geom[3] ) <br>
<br> def create_widgets(self):<br> self.label = QLabel("Stock:")<br> self.main_edit = QLineEdit()<br> self.main_button = QPushButton("Go") <br> QObject.connect(self.main_button<br>
, SIGNAL("clicked()")<br> , self.on_main_clicked1)<br> self.open_file = QPushButton("Open File") <br> QObject.connect(self.open_file<br> , SIGNAL("clicked()")<br>
, self.on_open_file)<br> QObject.connect(self.main_edit<br> , SIGNAL("returnPressed()")<br> , self.on_main_clicked1)<br> h_box = QHBoxLayout()<br> h_box.addWidget(self.label)<br>
h_box.addWidget(self.main_edit)<br> h_box.addWidget(self.main_button)<br> h_box.addWidget(self.open_file)<br> central_widget = QWidget()<br> central_widget.setLayout(h_box)<br> self.setCentralWidget(central_widget)<br>
<br> def on_open_file(self):<br> thread.start_new_thread(run_update_local, (self.subtable,))<br><br> def on_main_clicked1(self):<br> global last_time, num_prices<br> symbol = self.main_edit.displayText()<br>
symbol = str(symbol).upper()<br> popTable = MyWindow(symbol)<br> self.subtable[symbol] = popTable<br> last_time[symbol] = 0.<br> popTable.show()<br> popTable.thread = MyThread(symbol, popTable.tb)<br>
popTable.thread.toggle()<br> popTable.thread.start()<br> def closeEvent(self, event):<br> global port, local_flag<br> for s in self.subtable:<br> self.subtable[s].close()<br>
self.subtable[s].tb.close()<br> if not local_flag:<br> ps_proc = os.popen("ssh %s ps -ef | grep %s |awk '{ print $2 } '"%(server_get_feed, port), "r")<br>
s_proc = os.popen("ps -ef | grep %s | grep ssh | awk '{ print $2 } '"%port, "r")<br> for sp in s_proc: <br> os.system("kill -9 %d"%int(sp))<br>
for sp in ps_proc:<br> os.system("ssh %s kill -9 %d"%(server_get_feed, int(sp))) <br> l_proc = os.popen("ps -ef | grep %s | awk '{ print $2 } '"%port, "r")<br>
for sp in l_proc: <br> os.system("kill -9 %d"%int(sp))<br><br>class MyWindow(QWidget):<br> def __init__(self, symbol, *args):<br> global num_prices<br>
QWidget.__init__(self, *args)<br> self.tb = MyTable(symbol, num_prices, 5)<br> self.timer = QPushButton("Time: 00:00:00.000000", self)<br> self.re_center = QPushButton("ReCenter", self)<br>
self.clear = QPushButton("Clear", self)<br><br> layout = QVBoxLayout()<br> layout.addWidget(self.tb)<br> layout.addWidget(self.timer) <br> layout.addWidget(self.re_center)<br>
layout.addWidget(self.clear)<br> self.setLayout(layout)<br> self.setGeometry(table_Geom[0], table_Geom[1], table_Geom[2], table_Geom[3]) <br> self.setWindowTitle("%s"%symbol)<br><br>
<br> QWidget.connect(self.clear, SIGNAL("clicked()"), self.tb.clear_quotes)<br> QWidget.connect(self.re_center, SIGNAL("clicked()"), self.tb.recenter)<br><br>class MyTable(QTableWidget):<br>
def __init__(self, symbol, *args):<br> self.symbol = symbol<br> QTableWidget.__init__(self, *args)<br> self.setHorizontalHeaderLabels(" ")<br> self.verticalHeader().hide() <br>
self.setmydata()<br> font = QFont("Arial", 14)<br> font.setBold(True)<br> self.setFont(font)<br> # self.setGeometry(300,100,355,750) <br> self.setAutoScroll(True)<br>
<br> self.setColumnWidth(0,40)<br> self.setColumnWidth(1,70)<br> self.setColumnWidth(2,60)<br> self.setColumnWidth(3,70)<br> self.setColumnWidth(4,40)<br><br> self.buy_trades = {}<br>
self.sell_trades = {}<br> self.t = threading.Timer(20, self.recenter)<br> self.t.start()<br> self.dataset = {}<br> self.last_bs_data = []<br> self.best_buy = 0.<br> self.best_sell = 0.<br>
self.pricelist = []<br> self.last_pricelist = []<br> self.drawdata = []<br> self.drawprice = []<br> self.buy_quotes = {}<br> self.sell_quotes = {}<br> #print >>sys.stderr, "reached end"<br>
<br> def closeEvent(self, event):<br> self.t.cancel()<br><br> def set_newrow(self, n_idx): <br> self.insertRow(n_idx)<br> newitem = QTableWidgetItem(n_idx)<br> newitem.setSizeHint(QSize(100,32))<br>
newitem.setBackground(QBrush(QColor(153,153,153)))<br> self.setItem(n_idx, 2, newitem)<br> newitem = QTableWidgetItem(n_idx)<br> newitem.setSizeHint(QSize(100,32))<br> newitem.setBackground(QBrush(QColor(204,204,195)))<br>
self.setItem(n_idx, 0, newitem)<br> newitem = QTableWidgetItem(n_idx)<br> newitem.setSizeHint(QSize(100,32))<br> newitem.setBackground(QBrush(QColor(204,204,195))) <br> self.setItem(n_idx, 4, newitem)<br>
newitem = QTableWidgetItem(n_idx)<br> newitem.setSizeHint(QSize(100,32))<br> newitem.setBackground(QBrush(QColor(0,51,153)))<br> self.setItem(n_idx, 1, newitem)<br> newitem = QTableWidgetItem(n_idx)<br>
newitem.setSizeHint(QSize(100,32))<br> newitem.setBackground(QBrush(QColor(153,0,0)))<br> self.setItem(n_idx, 3, newitem) <br><br> def setmydata(self):<br> global num_prices<br>
n = 0<br> for n in xrange(5):<br> if n == 2:<br> for m in xrange(num_prices): <br> newitem = QTableWidgetItem(m)<br> newitem.setSizeHint(QSize(100,32))<br>
newitem.setBackground(QBrush(QColor(153,153,153)))<br> self.setItem(m, n, newitem)<br> elif n == 3:<br> for m in xrange(num_prices):<br> newitem = QTableWidgetItem(m)<br>
newitem.setText(" ")<br> newitem.setBackground(QBrush(QColor(153,0,0)))<br> self.setItem(m, n, newitem)<br> elif n == 1:<br> for m in xrange(num_prices):<br>
newitem = QTableWidgetItem(m)<br> newitem = QTableWidgetItem(m)<br> newitem.setText(" ")<br> newitem.setBackground(QBrush(QColor(0,51,153)))<br>
self.setItem(m, n, newitem)<br> else:<br> for m in xrange(num_prices):<br> newitem = QTableWidgetItem(m)<br> newitem = QTableWidgetItem(m)<br>
newitem.setText(" ")<br> newitem.setBackground(QBrush(QColor(204,204,195)))<br> self.setItem(m, n, newitem)<br><br> def draw_new(self):<br> lastlen = len(self.last_pricelist)<br>
newlen = len(self.pricelist) <br> if newlen > lastlen and newlen > 20:<br> if lastlen > 20:<br> for i in xrange(newlen - lastlen):<br> self.set_newrow(lastlen + i)<br>
else:<br> for i in xrange(newlen - 20):<br> self.set_newrow(20 + i)<br> elif lastlen > newlen: <br> if lastlen < 20:<br> for i in xrange(20 - newlen):<br>
self.clearline(newlen + i)<br> else:<br> if newlen < 20 or newlen == 20:<br> for i in xrange(lastlen - 20):<br> self.removeRow(20)<br>
for i in xrange(20 - newlen):<br> self.clearline(newlen + i)<br> else:<br> for i in xrange(lastlen - newlen):<br> self.removeRow(newlen)<br>
<br> for i in xrange(len(self.drawprice)):<br> if self.drawprice[i] != 0:<br> self.item(i, 2).setText("%.02f"%self.drawprice[i])<br> self.item(i, 2).setForeground(QBrush(QColor(255,255,255)))<br>
for i in xrange(len(self.drawdata)):<br> for j in xrange(4):<br> if j == 0:<br> if self.drawdata[i][j] == -1:<br> self.item(i, 1).setText(" ")<br>
self.item(i, 1).setBackground(QBrush(QColor(0,51,153)))<br> elif self.drawdata[i][j] != 0:<br> self.item(i, 1).setText("%d"%self.drawdata[i][j])<br>
self.item(i, 1).setForeground(QBrush(QColor(255,255,255))) <br> self.item(i, 1).setBackground(QBrush(QColor(20,80,153))) <br> if j == 1:<br> if self.drawdata[i][j] == -1:<br>
self.item(i, 3).setText(" ")<br> self.item(i, 3).setBackground(QBrush(QColor(153,0,0)))<br> elif self.drawdata[i][j] != 0:<br> self.item(i, 3).setText("%d"%self.drawdata[i][j])<br>
self.item(i, 3).setForeground(QBrush(QColor(255,255,255))) <br> self.item(i, 3).setBackground(QBrush(QColor(204,0,0))) <br> if j == 2:<br> if self.drawdata[i][j] == -1:<br>
self.item(i, 0).setText(" ")<br> self.item(i, 0).setBackground(QBrush(QColor(204,204,195)))<br> self.item(i, 2).setBackground(QBrush(QColor(153,153,153)))<br>
elif self.drawdata[i][j] != 0:<br> self.item(i, 0).setText("%d"%self.drawdata[i][j])<br> self.item(i, 0).setBackground(QBrush(QColor(0,255,0))) <br>
self.item(i, 2).setBackground(QBrush(QColor(0,51,153))) <br> if j == 3:<br> if self.drawdata[i][j] == -1:<br> self.item(i, 4).setText(" ")<br>
self.item(i, 4).setBackground(QBrush(QColor(204,204,195)))<br> self.item(i, 2).setBackground(QBrush(QColor(153,153,153)))<br> elif self.drawdata[i][j] != 0:<br>
self.item(i, 4).setText("%d"%self.drawdata[i][j])<br> self.item(i, 4).setBackground(QBrush(QColor(0,255,0))) <br> self.item(i, 2).setBackground(QBrush(QColor(204,0,0))) <br>
time.sleep(0.0000001)<br> # print self.drawdata<br> # print self.drawprice<br><br> def recenter(self):<br> print >> GUI_TEST_LOG, "LINE %d recentering"%lineno(), self.best_buy<br>
try:<br> index = self.price_idx[self.best_buy] + len(self.high_outliers) <br> self.scrollToItem(self.item(index, int(2)), QAbstractItemView.PositionAtCenter)<br> except:<br>
pass<br> t = threading.Timer(20, self.recenter).start()<br><br><br><b>#0 0x00002b481c6b2f93 in QVector<QLayoutStruct>::realloc (this=0x7fffcff2f990, asize=1398090296, aalloc=0)<br> at ../../include/QtCore/qvector.h:502<br>
#1 0x00002b481c6d3c33 in qGeomCalc (chain=..., start=0, count=4, pos=11, space=737, spacer=-1) at ../../include/QtCore/qvector.h:134<br>#2 0x00002b481c6b05c8 in QBoxLayout::setGeometry (this=0x16470dd0, r=...) at kernel/qboxlayout.cpp:850<br>
#3 0x00002b481c05230f in ?? () from /usr/local/lib/python2.7/site-packages/PyQt4/QtGui.so<br>#4 0x00002b481c6d16da in QLayoutPrivate::doResize (this=0x16470e20, r=...) at kernel/qlayout.cpp:681<br>#5 0x00002b481c6d1fcf in QLayout::activate (this=0x16470dd0) at kernel/qlayout.cpp:1259<br>
#6 0x00002b481c6a00f2 in QApplicationPrivate::notify_helper (this=0x160952a0, receiver=0x163075e0, e=0x2aaaac00a3c0)<br> at kernel/qapplication.cpp:4293<br>#7 0x00002b481c6a9ca2 in QApplication::notify (this=0x16094750, receiver=0x163075e0, e=0x2aaaac00a3c0) at kernel/qapplication.cpp:3736<br>
#8 0x00002b481c063144 in ?? () from /usr/local/lib/python2.7/site-packages/PyQt4/QtGui.so<br>#9 0x00002b481b6407e4 in QCoreApplication::notifyInternal (this=0x16094750, receiver=0x163075e0, event=0x2aaaac00a3c0)<br> at kernel/qcoreapplication.cpp:726<br>
#10 0x00002b481b64154d in QCoreApplicationPrivate::sendPostedEvents (receiver=0x0, event_type=0, data=0x160885c0)<br> at ../../include/QtCore/qcoreapplication.h:215<br>#11 0x00002b481b66ab83 in sendPostedEvents (s=<value optimized out>) at ../../include/QtCore/qcoreapplication.h:220<br>
#12 postEventSourceDispatch (s=<value optimized out>) at kernel/qeventdispatcher_glib.cpp:276<br>#13 0x0000003d55a2cdb4 in g_main_context_dispatch () from /lib64/libglib-2.0.so.0<br>#14 0x0000003d55a2fc0d in ?? () from /lib64/libglib-2.0.so.0<br>
#15 0x0000003d55a3011e in g_main_context_iteration () from /lib64/libglib-2.0.so.0<br>#16 0x00002b481b66ade5 in QEventDispatcherGlib::processEvents (this=0x16101320, flags=...) at kernel/qeventdispatcher_glib.cpp:412<br>#17 0x00002b481c74a94f in QGuiEventDispatcherGlib::processEvents (this=0x0, flags=<value optimized out>)<br>
at kernel/qguieventdispatcher_glib.cpp:204<br>#18 0x00002b481b63f975 in QEventLoop::processEvents (this=<value optimized out>, flags=...) at kernel/qeventloop.cpp:149<br>#19 0x00002b481b63fccd in QEventLoop::exec (this=0x7fffcff30350, flags=...) at kernel/qeventloop.cpp:201<br>
#20 0x00002b481b6418e4 in QCoreApplication::exec () at kernel/qcoreapplication.cpp:1003<br>#21 0x00002b481c063e3d in ?? () from /usr/local/lib/python2.7/site-packages/PyQt4/QtGui.so<br>#22 0x000000000049b574 in call_function (f=0x15c91170, throwflag=<value optimized out>) at Python/ceval.c:4012<br>
#23 PyEval_EvalFrameEx (f=0x15c91170, throwflag=<value optimized out>) at Python/ceval.c:2665<br>#24 0x000000000049d3bb in PyEval_EvalCodeEx (co=0x2b48178c7a30, globals=<value optimized out>, locals=<value optimized out>, args=0x0, <br>
argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:3252<br>#25 0x000000000049d432 in PyEval_EvalCode (co=0x0, globals=0x0, locals=0x164b8840) at Python/ceval.c:666<br>#26 0x00000000004bf321 in run_mod (fp=0x15c6b300, filename=0x7fffcff31bdd "/linden/bin/gui_depthbook.py", start=<value optimized out>, <br>
globals=0x15be9f50, locals=0x15be9f50, closeit=1, flags=0x7fffcff30800) at Python/pythonrun.c:1346<br>#27 PyRun_FileExFlags (fp=0x15c6b300, filename=0x7fffcff31bdd "/linden/bin/gui_depthbook.py", start=<value optimized out>, globals=0x15be9f50, <br>
locals=0x15be9f50, closeit=1, flags=0x7fffcff30800) at Python/pythonrun.c:1332<br>#28 0x00000000004bf5d8 in PyRun_SimpleFileExFlags (fp=<value optimized out>, filename=0x7fffcff31bdd "gui_depthbook.py", <br>
closeit=1, flags=0x7fffcff30800) at Python/pythonrun.c:936<br>#29 0x00000000004148cc in Py_Main (argc=<value optimized out>, argv=<value optimized out>) at Modules/main.c:599<br>#30 0x0000003d5321d994 in __libc_start_main () from /lib64/libc.so.6<br>
#31 0x0000000000413b19 in _start ()</b><br>