Skip to content

Remove references to event loop policies. #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions qasync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,29 +812,27 @@ def wrapper(*args, **kwargs):

return outer_decorator


class QEventLoopPolicyMixin:
def new_event_loop(self):
return QEventLoop(QApplication.instance() or 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)
def _use_qeventloop(loop_factory):
app = QApplication.instance() or QApplication([sys.argv])
if loop_factory is None:
loop = QEventLoop(app)
else:
loop = loop_factory(app)
old_loop = asyncio.get_event_loop()
asyncio.set_event_loop(loop)
try:
yield
yield loop
finally:
asyncio.set_event_loop_policy(old_policy)

loop.close()
asyncio.set_event_loop(old_loop)

def run(*args, **kwargs):
with _set_event_loop_policy(DefaultQEventLoopPolicy()):
return asyncio.run(*args, **kwargs)
# A run function matching the signature of asyncio.run
def run(main_coro, *, debug=None, loop_factory=None):
"""
Run the given coroutine using a QEventLoop.
"""
with _use_qeventloop(loop_factory) as loop:
if debug is not None:
loop.set_debug(debug)
return loop.run_until_complete(main_coro)