PyQT线程杀死了我的记忆

时间:2015-03-11 00:46:48

标签: multithreading user-interface pyqt pyside qthread

以下代码块是类Dataview(QWidget)的一部分,它在main.py中调用。它创建了3个表; 出价,询问和历史记录并通过API自动从各种交易所获取数据。

该应用程序可以工作,但在任务管理器中查看时,我的内存使用量越来越大 - 我认为这是由于我运行这些线程的方式。我在这里做错了什么?

编辑:

MAIN.PY

import sys
from PyQt4 import QtCore, QtGui, Qt
from datetime import datetime
import time
import thread
from apis import API_Bitstamp_usd

class Dataview(QtGui.QFrame):
def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)

    self.setFixedSize(152,730)
    self.setStyleSheet('background: #444;background-color: QLinearGradient(x1:0, y1:0,x2:0, y2:1,stop:1 #212121,stop:0.4 #343434/*,stop:0.2 #343434,stop:0.1 #ffaa00*/);margin-bottom:-1px;padding-bottom:1px;')
    self.setup_table_asks()
    self.setup_table_bids()
    self.setup_table_hist()
    self.setup_label()
    self.start_threads()

def setup_table_asks(self):
    self.table_asks = QtGui.QTableWidget(self)
    self.table_asks.setGeometry(10,30,132,180)
    self.table_asks.setStyleSheet('color: lightblue; background: #444;background-color: QLinearGradient(x1:0, y1:0,x2:0, y2:1,stop:1 #212121,stop:0.4 #343434/*,stop:0.2 #343434,stop:0.1 #ffaa00*/);margin-bottom:-1px;padding-bottom:1px;')
    self.table_asks.setFrameShadow(QtGui.QFrame.Raised)
    self.table_asks.horizontalHeader().hide()
    self.table_asks.verticalHeader().hide()
    self.table_asks.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.table_asks.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.table_asks.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
    self.table_asks.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
    self.table_asks.setShowGrid(False)
    self.table_asks.setRowCount(300)
    self.table_asks.setColumnCount(3)
    self.table_asks.setColumnWidth(0,50)
    self.table_asks.setColumnWidth(1,40)
    self.table_asks.setColumnWidth(2,40)
    self.table_asks.setCursor(QtCore.Qt.SizeVerCursor)
    self.table_asks.scrollToBottom()

def setup_table_bids(self):

    self.table_bids = QtGui.QTableWidget(self)
    self.table_bids.setGeometry(10,230,132,180)
    self.table_bids.setStyleSheet('color: lightblue; background: #444;background-color: QLinearGradient(x1:0, y1:0,x2:0, y2:1,stop:1 #212121,stop:0.4 #343434/*,stop:0.2 #343434,stop:0.1 #ffaa00*/);margin-bottom:-1px;padding-bottom:1px;')
    self.table_bids.setFrameShadow(QtGui.QFrame.Raised)
    self.table_bids.horizontalHeader().hide()
    self.table_bids.verticalHeader().hide()
    self.table_bids.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.table_bids.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.table_bids.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
    self.table_bids.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
    self.table_bids.setShowGrid(False)
    self.table_bids.setRowCount(300)
    self.table_bids.setColumnCount(3)
    self.table_bids.setColumnWidth(0,50)
    self.table_bids.setColumnWidth(1,40)
    self.table_bids.setColumnWidth(2,40)
    self.table_bids.setCursor(QtCore.Qt.SizeVerCursor)

def setup_table_hist(self):
    self.table_hist = QtGui.QTableWidget(self)
    self.table_hist.setGeometry(10,414,132,206)
    self.table_hist.setStyleSheet('color: lightblue;background: #444;background-color: QLinearGradient(x1:0, y1:0,x2:0, y2:1,stop:1 #212121,stop:0.4 #343434/*,stop:0.2 #343434,stop:0.1 #ffaa00*/);margin-bottom:-1px;padding-bottom:1px;')
    self.table_hist.setFrameShadow(QtGui.QFrame.Raised)
    self.table_hist.horizontalHeader().hide()
    self.table_hist.verticalHeader().hide()
    self.table_hist.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.table_hist.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
    self.table_hist.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
    self.table_hist.setSelectionMode(QtGui.QAbstractItemView.NoSelection)
    self.table_hist.setShowGrid(False)
    self.table_hist.setRowCount(150)
    self.table_hist.setColumnCount(3)
    self.table_hist.setColumnWidth(0,45)
    self.table_hist.setColumnWidth(1,45)
    self.table_hist.setColumnWidth(2,40)
    self.table_hist.setCursor(QtCore.Qt.SizeVerCursor)

def setup_label(self):
    self.label = QtGui.QLabel(self)
    self.label.setGeometry(10,210,132,20)
    self.label.setFrameShadow(QtGui.QFrame.Sunken)
    self.label.setText('last_price')
    self.label.setStyleSheet('color: lightblue;background: #444;background-color: QLinearGradient(x1:0, y1:0,x2:0, y2:1,stop:1 #212121,stop:0.4 #343434/*,stop:0.2 #343434,stop:0.1 #ffaa00*/);margin-bottom:-1px;padding-bottom:1px;')

    self.label.setAlignment(QtCore.Qt.AlignCenter)
    self.title = QtGui.QLabel(self)
    self.title.setGeometry(10,10,132,20)
    self.title.setStyleSheet('color: silver;background: #444;background-color: QLinearGradient(x1:0, y1:0,x2:0, y2:1,stop:1 #212121,stop:0.4 #343434/*,stop:0.2 #343434,stop:0.1 #ffaa00*/);margin-bottom:-1px;padding-bottom:1px;')
    self.title.setText('Bitstamp')
    self.title.setAlignment(QtCore.Qt.AlignCenter)



def start_threads(self):
    thread.start_new_thread(self.fill_table_bids, ())
    thread.start_new_thread(self.fill_table_asks, ())
    thread.start_new_thread(self.fill_table_hist, ())

def fill_table_bids(self):
    while True:
        ticker = API_Bitstamp_usd()
        x = ticker.get_depth()
        bids = x[0]
        row = 0
        depth = 0
        for i in bids:
            price = i[0]
            qty = i[1]
            depth = format(float(depth) + float(qty), '.1f')
            self.table_bids.setRowHeight(row, 12)
            tw = QtGui.QTableWidgetItem(str(price))
            self.table_bids.setItem(row,0,tw)
            tw = QtGui.QTableWidgetItem(str(qty))
            tw.setTextAlignment(5)
            self.table_bids.setItem(row,1,tw)
            tw = QtGui.QTableWidgetItem(str(depth))
            tw.setTextAlignment(5)
            self.table_bids.setItem(row,2,tw)
            row = row + 1
        time.sleep(2)

def fill_table_asks(self):
    while True:
        ticker = API_Bitstamp_usd()
        x = ticker.get_depth()
        asks = x[1]
        row = 0
        depth = 0
        for i in asks[:300]:
            depth = depth + float(i[1])
        for i in reversed(asks[:300]):
            price, qty = i[0], i[1]
            depth = format(float(depth) - float(qty), '.1f')
            self.table_asks.setRowHeight(row, 12)
            tw = QtGui.QTableWidgetItem(str(price))
            self.table_asks.setItem(row,0,tw)
            tw = QtGui.QTableWidgetItem(str(qty))
            tw.setTextAlignment(5)
            self.table_asks.setItem(row,1,tw)
            if depth > 0:
                tw = QtGui.QTableWidgetItem(str(depth))
                tw.setTextAlignment(5)
                self.table_asks.setItem(row,2,tw)
            row = row + 1
        time.sleep(2)

def fill_table_hist(self):
    while True:
        ticker = API_Bitstamp_usd()
        x = ticker.get_history()
        row = 0
        for i in x:
            timestamp = datetime.fromtimestamp(float(i[0])).strftime('%H:%M:%S')
            price = i[1]
            qty = i[2]
            type = i[3]
            self.table_hist.setRowHeight(row, 12)
            tw = QtGui.QTableWidgetItem(str(timestamp))
            self.table_hist.setItem(row,0,tw)
            tw = QtGui.QTableWidgetItem(str(price))
            tw.setTextAlignment(5)
            self.table_hist.setItem(row,1,tw)
            tw = QtGui.QTableWidgetItem(str(qty))
            tw.setTextAlignment(5)
            if type == 'sell':
                tw.setTextColor(QtGui.QColor('red'))
            else:
                tw.setTextColor(QtGui.QColor('green'))
            self.table_hist.setItem(row,2,tw)
            row = row + 1
        self.label.setText(str(x[0][1]))
        time.sleep(2)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)

    myapp = Dataview()
    myapp.show()
    sys.exit(app.exec_())

APIS.PY

import urllib2
import json

class API_Bitstamp_usd:

    def __init__(self):
        self.url = 'https://www.bitstamp.net/api/'

    def get_depth(self):
        try:
            url = self.url + 'order_book/'
            json_obj = urllib2.urlopen(url)
            data = json.load(json_obj)
            asks_, bids_ = [], []
            for i in data['bids']:
                price, qty = format(float(i[0]), '.2f'), format(float(i[1]), '.2f')
                bids_.append([float(price), float(qty)])
            for i in data['asks']:
                price, qty = format(float(i[0]), '.2f'), format(float(i[1]), '.2f')
                asks_.append([float(price), float(qty)])
            return bids_, asks_
        except:
            pass

    def get_history(self):
        try:
            url = self.url + 'transactions/'
            json_obj = urllib2.urlopen(url)
            data = json.load(json_obj)
            history_ = []
            for i in data:
                timestamp, price, amount, type = i['date'], format(float(i['price']), '.3f'), format(float(i['amount']), '.2f'), ''
                history_.append([timestamp, float(price), float(amount), type])
            return history_

        except:
            pass

0 个答案:

没有答案