diff --git a/examples/aiohttp_fetch.py b/examples/aiohttp_fetch.py index 7de9e44..78eb029 100644 --- a/examples/aiohttp_fetch.py +++ b/examples/aiohttp_fetch.py @@ -1,22 +1,28 @@ -import sys import asyncio import aiohttp -from qasync import run, asyncSlot, asyncClose # from PyQt5.QtWidgets import ( from PySide2.QtWidgets import ( - QWidget, QLabel, QLineEdit, QTextEdit, QPushButton, - QVBoxLayout) + QWidget, + QLabel, + QLineEdit, + QTextEdit, + QPushButton, + QVBoxLayout, +) + +import qasync +from qasync import asyncSlot, asyncClose class MainWindow(QWidget): """Main window.""" - _DEF_URL = 'https://jsonplaceholder.typicode.com/todos/1' + _DEF_URL = "https://jsonplaceholder.typicode.com/todos/1" """str: Default URL.""" - _SESSION_TIMEOUT = 1. + _SESSION_TIMEOUT = 1.0 """float: Session timeout.""" def __init__(self): @@ -24,22 +30,23 @@ def __init__(self): self.setLayout(QVBoxLayout()) - self.lblStatus = QLabel('Idle', self) + self.lblStatus = QLabel("Idle", self) self.layout().addWidget(self.lblStatus) self.editUrl = QLineEdit(self._DEF_URL, self) self.layout().addWidget(self.editUrl) - self.editResponse = QTextEdit('', self) + self.editResponse = QTextEdit("", self) self.layout().addWidget(self.editResponse) - self.btnFetch = QPushButton('Fetch', self) + self.btnFetch = QPushButton("Fetch", self) self.btnFetch.clicked.connect(self.on_btnFetch_clicked) self.layout().addWidget(self.btnFetch) self.session = aiohttp.ClientSession( loop=asyncio.get_event_loop(), - timeout=aiohttp.ClientTimeout(total=self._SESSION_TIMEOUT)) + timeout=aiohttp.ClientTimeout(total=self._SESSION_TIMEOUT), + ) @asyncClose async def closeEvent(self, event): @@ -48,15 +55,15 @@ async def closeEvent(self, event): @asyncSlot() async def on_btnFetch_clicked(self): self.btnFetch.setEnabled(False) - self.lblStatus.setText('Fetching...') + self.lblStatus.setText("Fetching...") try: async with self.session.get(self.editUrl.text()) as r: self.editResponse.setText(await r.text()) except Exception as exc: - self.lblStatus.setText('Error: {}'.format(exc)) + self.lblStatus.setText("Error: {}".format(exc)) else: - self.lblStatus.setText('Finished!') + self.lblStatus.setText("Finished!") finally: self.btnFetch.setEnabled(True) diff --git a/examples/executor_example.py b/examples/executor_example.py index 6ca6f92..9e242a2 100644 --- a/examples/executor_example.py +++ b/examples/executor_example.py @@ -1,10 +1,11 @@ +import functools import sys import asyncio import time +import qasync # from PyQt5.QtWidgets import ( -from PySide2.QtWidgets import ( - QApplication, QProgressBar) +from PySide2.QtWidgets import QApplication, QProgressBar from qasync import QEventLoop, QThreadExecutor @@ -13,22 +14,22 @@ async def master(): progress.setRange(0, 99) progress.show() - await first_50() + await first_50(progress) loop = asyncio.get_running_loop() with QThreadExecutor(1) as exec: - await loop.run_in_executor(exec, last_50, loop) + await loop.run_in_executor(exec, functools.partial(last_50, progress), loop) -async def first_50(): +async def first_50(progress): for i in range(50): progress.setValue(i) - await asyncio.sleep(.1) + await asyncio.sleep(0.1) -def last_50(loop): +def last_50(progress, loop): for i in range(50, 100): loop.call_soon_threadsafe(progress.setValue, i) - time.sleep(.1) + time.sleep(0.1) qasync.run(master())