Skip to content

Commit

Permalink
Fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGreatCabbage committed Mar 14, 2021
1 parent f1de255 commit e96fa84
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
33 changes: 20 additions & 13 deletions examples/aiohttp_fetch.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,52 @@
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):
super().__init__()

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):
Expand All @@ -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)

Expand Down
17 changes: 9 additions & 8 deletions examples/executor_example.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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())

0 comments on commit e96fa84

Please sign in to comment.