Skip to content

Commit

Permalink
Merge pull request #29 by @graingert
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGreatCabbage committed Mar 14, 2021
2 parents aa4db14 + 280f939 commit f1de255
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ install:
- pipenv run pip install PyQt5 PySide2

script:
- QT_API=PyQt5 pipenv run py.test -v
- QT_API=PySide2 pipenv run py.test -v
- QT_API=PyQt5 pipenv run py.test --forked -v
- QT_API=PySide2 pipenv run py.test --forked -v
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ verify_ssl = true
pytest = "*"
pathlib2 = "*"
atomicwrites = "*"
pytest-forked = "*"

[packages]

Expand Down
25 changes: 24 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions examples/aiohttp_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import asyncio

import aiohttp
from qasync import QEventLoop, asyncSlot, asyncClose
from qasync import run, asyncSlot, asyncClose

# from PyQt5.QtWidgets import (
from PySide2.QtWidgets import (
QApplication, QWidget, QLabel, QLineEdit, QTextEdit, QPushButton,
QWidget, QLabel, QLineEdit, QTextEdit, QPushButton,
QVBoxLayout)


Expand Down Expand Up @@ -61,13 +61,12 @@ async def on_btnFetch_clicked(self):
self.btnFetch.setEnabled(True)


if __name__ == '__main__':
app = QApplication(sys.argv)
loop = QEventLoop(app)
asyncio.set_event_loop(loop)

async def main():
loop = asyncio.get_running_loop()
mainWindow = MainWindow()
mainWindow.show()
await loop.create_future()


with loop:
sys.exit(loop.run_forever())
if __name__ == "__main__":
qasync.run(main())
21 changes: 8 additions & 13 deletions examples/executor_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@
from qasync import QEventLoop, QThreadExecutor


app = QApplication(sys.argv)
loop = QEventLoop(app)
asyncio.set_event_loop(loop)

progress = QProgressBar()
progress.setRange(0, 99)
progress.show()


async def master():
progress = QProgressBar()
progress.setRange(0, 99)
progress.show()

await first_50()
loop = asyncio.get_running_loop()
with QThreadExecutor(1) as exec:
await loop.run_in_executor(exec, last_50)
await loop.run_in_executor(exec, last_50, loop)


async def first_50():
Expand All @@ -29,11 +25,10 @@ async def first_50():
await asyncio.sleep(.1)


def last_50():
def last_50(loop):
for i in range(50, 100):
loop.call_soon_threadsafe(progress.setValue, i)
time.sleep(.1)


with loop:
loop.run_until_complete(master())
qasync.run(master())
33 changes: 30 additions & 3 deletions qasync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
__all__ = ["QEventLoop", "QThreadExecutor", "asyncSlot", "asyncClose"]

import asyncio
import contextlib
import functools
import importlib
import itertools
Expand Down Expand Up @@ -532,9 +533,8 @@ def __notifier_cb_wrapper(self, notifiers, notifier, fd, callback, args):
def __on_notifier_ready(self, notifiers, notifier, fd, callback, args):
if fd not in notifiers:
self._logger.warning(
"Socket notifier for fd {} is ready, even though it should be disabled, not calling {} and disabling".format(
fd, callback
),
f"Socket notifier for fd {fd} is ready, even though it should "
f"be disabled, not calling {callback} and disabling"
)
notifier.setEnabled(False)
return
Expand Down Expand Up @@ -727,3 +727,30 @@ def wrapper(*args, **kwargs):
return wrapper

return outer_decorator


class QEventLoopPolicyMixin:
def new_event_loop(self):
return QEventLoop(QApplication(sys.argv))


class DefaultQEventLoopPolicy(
QEventLoopPolicyMixin,
asyncio.DefaultEventLoopPolicy,
):
pass


@contextlib.contextmanager
def _set_event_loop_policy(policy):
old_policy = asyncio.get_event_loop_policy()
asyncio.set_event_loop_policy(policy)
try:
yield
finally:
asyncio.set_event_loop_policy(old_policy)


def run(*args, **kwargs):
with _set_event_loop_policy(DefaultQEventLoopPolicy()):
return asyncio.run(*args, **kwargs)

0 comments on commit f1de255

Please sign in to comment.