diff --git a/src/cutecoin/core/net/api/bma/__init__.py b/src/cutecoin/core/net/api/bma/__init__.py index adaa1dcaa36d8087e5b1a3e3e4a4f1ff6bbf0104..c6d0f2b51ac0b1cb7b471b5fb477adde93e5f113 100644 --- a/src/cutecoin/core/net/api/bma/__init__.py +++ b/src/cutecoin/core/net/api/bma/__init__.py @@ -3,13 +3,22 @@ __all__ = ['api'] from PyQt5.QtNetwork import QNetworkRequest, QNetworkReply -from PyQt5.QtCore import QUrl, QUrlQuery, QTimer +from PyQt5.QtCore import QUrl, QUrlQuery, QTimer, QObject, pyqtSlot import logging +import asyncio logger = logging.getLogger("ucoin") PROTOCOL_VERSION = "1" +@asyncio.coroutine +def timeout(reply, seconds): + logging.debug("Sleep timeout...") + yield from asyncio.sleep(seconds) + if reply.isRunning(): + logging.debug("Reply aborted because of timeout") + reply.abort() + class ConnectionHandler(object): """Helper class used by other API classes to ease passing server connection information.""" @@ -99,18 +108,7 @@ class API(object): logging.debug(url.toString()) reply = self.conn_handler.network_manager.get(request) - - def onTimeout(reply): - logging.debug("Timeout error on reply") - reply.setError(QNetworkReply.TimeoutError, "Timeout error") - reply.abort() - - timer = QTimer() - timer.setInterval(100) - timer.timeout.connect(lambda: onTimeout(reply)) - timer.start() - #reply.downloadProgress.connect(lambda: timer.start(10000)) - reply.finished.connect(timer.stop) + asyncio.async(timeout(reply, 15)) return reply @@ -137,12 +135,7 @@ class API(object): reply = self.conn_handler.network_manager.post(request, post_data.toString(QUrl.FullyEncoded).encode('utf-8')) logging.debug(url.toString(QUrl.FullyEncoded)) - timer = QTimer() - timer.setInterval(15000) - timer.timeout.connect(reply.abort) - reply.downloadProgress.connect(lambda: timer.start(15000)) - reply.finished.connect(timer.stop) - + asyncio.async(timeout(reply, 15)) return reply from . import network, blockchain, tx, wot, ud, node \ No newline at end of file