Skip to content
Snippets Groups Projects
Commit 7a2e3a8a authored by inso's avatar inso
Browse files

Threading and parallelism

Added threading of account loading and blockchain watching so that the UI doesn't freeze anymore
parent b11e09bd
No related branches found
No related tags found
No related merge requests found
......@@ -100,48 +100,6 @@
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>6</number>
</property>
<item>
<widget class="QLabel" name="label_current_block">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QProgressBar" name="bar_requests">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximum">
<number>0</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="textVisible">
<bool>true</bool>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="invertedAppearance">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
......
......@@ -30,8 +30,6 @@ class Application(object):
self.current_account = None
config.parse_arguments(argv)
self.load()
if self.default_account != "":
self.change_current_account(self.get_account(self.default_account))
def get_account(self, name):
if not self.accounts[name]:
......
......@@ -127,8 +127,13 @@ class Community(object):
hash(tuple(frozenset(sorted(get_args.items())))))
if cache_key not in self.requests_cache.keys():
logging.debug("Connecting to {0}:{1}".format(e.server,
if e.server:
logging.debug("Connecting to {0}:{1}".format(e.server,
e.port))
else:
logging.debug("Connecting to {0}:{1}".format(e.ipv4,
e.port))
req = request(e.conn_handler(), **req_args)
data = req.get(**get_args)
if inspect.isgenerator(data):
......
......@@ -9,7 +9,7 @@ import time
from ucoinpy.api import bma
from PyQt5.QtWidgets import QWidget, QMenu, QAction, QApplication
from PyQt5.QtCore import QModelIndex, Qt, pyqtSlot, QThread, pyqtSignal
from PyQt5.QtCore import QModelIndex, Qt, pyqtSlot, QObject, QThread, pyqtSignal
from PyQt5.QtGui import QIcon
from ..gen_resources.currency_tab_uic import Ui_CurrencyTabWidget
from .community_tab import CommunityTabWidget
......@@ -19,19 +19,16 @@ from ..models.wallets import WalletsListModel
from ..models.wallet import WalletListModel
class BlockchainInspector(QThread):
class BlockchainWatcher(QObject):
def __init__(self, account, community):
QThread.__init__(self)
super().__init__()
self.account = account
self.community = community
self.exiting = False
self.last_block = self.community.request(bma.blockchain.Current)['number']
def __del__(self):
self.exiting = True
self.wait()
def run(self):
@pyqtSlot()
def watch(self):
while not self.exiting:
time.sleep(10)
current_block = self.community.request(bma.blockchain.Current)
......@@ -53,7 +50,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
classdocs
'''
def __init__(self, app, community, password_asker):
def __init__(self, app, community, password_asker, status_label):
'''
Constructor
'''
......@@ -62,13 +59,19 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
self.app = app
self.community = community
self.password_asker = password_asker
self.status_label = status_label
self.tab_community = CommunityTabWidget(self.app.current_account,
self.community,
self.password_asker)
self.bc_inspector = BlockchainInspector(self.app.current_account,
self.bc_watcher = BlockchainWatcher(self.app.current_account,
community)
self.bc_inspector.new_block_mined.connect(self.refresh_block)
self.bc_inspector.start()
self.bc_watcher.new_block_mined.connect(self.refresh_block)
self.watcher_thread = QThread()
self.bc_watcher.moveToThread(self.watcher_thread)
self.watcher_thread.started.connect(self.bc_watcher.watch)
self.watcher_thread.start()
def refresh(self):
if self.app.current_account is None:
......@@ -88,7 +91,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
QIcon(':/icons/community_icon'),
"Community")
block_number = self.community.request(bma.blockchain.Current)['number']
self.label_current_block.setText("Current Block : {0}"
self.status_label.setText("Connected : Block {0}"
.format(block_number))
@pyqtSlot(int)
......@@ -116,8 +119,8 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
QModelIndex(),
[])
self.label_current_block.setText("Current Block : {0}"
.format(block_number))
self.label_current_block.setText("Connected : Block {0}"
.format(block_number))
def refresh_wallets(self):
wallets_list_model = WalletsListModel(self.app.current_account,
......@@ -162,3 +165,12 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
def wallet_changed(self):
self.app.save(self.app.current_account)
def showEvent(self, event):
block_number = self.community.request(bma.blockchain.Current)['number']
self.status_label.setText("Connected : Block {0}"
.format(block_number))
def closeEvent(self, event):
self.bc_watcher.deleteLater()
self.watcher_thread.deleteLater()
......@@ -4,8 +4,8 @@ Created on 1 févr. 2014
@author: inso
'''
from cutecoin.gen_resources.mainwindow_uic import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow, QAction, QFileDialog
from PyQt5.QtCore import QSignalMapper, QModelIndex, QThread, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QAction, QFileDialog, QProgressBar, QLabel
from PyQt5.QtCore import QSignalMapper, QModelIndex, QObject, QThread, pyqtSlot, pyqtSignal
from PyQt5.QtGui import QIcon
from .process_cfg_account import ProcessConfigureAccount
from .transfer import TransferMoneyDialog
......@@ -18,6 +18,24 @@ from .password_asker import PasswordAskerDialog
import logging
class Loader(QObject):
def __init__(self, app):
super().__init__()
self.app = app
self.account_name = ""
loaded = pyqtSignal()
def set_account_name(self, name):
self.account_name = name
@pyqtSlot()
def load(self):
if self.account_name != "":
self.app.change_current_account(self.app.get_account(self.account_name))
self.loaded.emit()
class MainWindow(QMainWindow, Ui_MainWindow):
'''
......@@ -33,6 +51,23 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.setupUi(self)
self.app = app
self.password_asker = None
self.initialized = False
self.busybar = QProgressBar(self.statusbar)
self.busybar.setMinimum(0)
self.busybar.setMaximum(0)
self.busybar.setValue(-1)
self.statusbar.addWidget(self.busybar)
self.busybar.hide()
self.status_label = QLabel("", self.statusbar)
self.statusbar.addPermanentWidget(self.status_label)
self.loader_thread = QThread()
self.loader = Loader(self.app)
self.loader.moveToThread(self.loader_thread)
self.loader.loaded.connect(self.loader_finished)
self.loader.loaded.connect(self.loader_thread.quit)
self.loader_thread.started.connect(self.loader.load)
self.refresh()
def open_add_account_dialog(self):
......@@ -40,9 +75,16 @@ class MainWindow(QMainWindow, Ui_MainWindow):
dialog.accepted.connect(self.refresh)
dialog.exec_()
def action_change_account(self, account_name):
self.app.change_current_account(self.app.get_account(account_name))
@pyqtSlot()
def loader_finished(self):
self.refresh()
self.busybar.hide()
def action_change_account(self, account_name):
self.busybar.show()
self.status_label.setText("Loading account {0}".format(account_name))
self.loader.set_account_name(account_name)
self.loader_thread.start(QThread.LowPriority)
def open_transfer_money_dialog(self):
dialog = TransferMoneyDialog(self.app.current_account,
......@@ -112,7 +154,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.currencies_tabwidget.clear()
for community in self.app.current_account.communities:
tab_currency = CurrencyTabWidget(self.app, community,
self.password_asker)
self.password_asker,
self.status_label)
tab_currency.refresh()
self.currencies_tabwidget.addTab(tab_currency,
QIcon(":/icons/currency_icon"),
......@@ -142,5 +185,14 @@ class MainWindow(QMainWindow, Ui_MainWindow):
def closeEvent(self, event):
if self.app.current_account:
self.app.save_cache(self.app.current_account)
self.loader.deleteLater()
self.loader_thread.deleteLater()
super().closeEvent(event)
def showEvent(self, event):
super().showEvent(event)
if not self.initialized:
if self.app.default_account != "":
logging.debug("Loading default account")
self.action_change_account(self.app.default_account)
self.initialized = True
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment