-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental async implementation (#401)
- Loading branch information
Showing
20 changed files
with
1,577 additions
and
1,547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import asyncio | ||
import contextlib | ||
import functools | ||
from typing import Callable, Coroutine | ||
|
||
from gi.events import GLibEventLoopPolicy | ||
|
||
|
||
@contextlib.contextmanager | ||
def glib_event_loop_policy(): | ||
original = asyncio.get_event_loop_policy() | ||
policy = GLibEventLoopPolicy() | ||
asyncio.set_event_loop_policy(policy) | ||
try: | ||
yield policy | ||
finally: | ||
asyncio.set_event_loop_policy(original) | ||
|
||
|
||
_background_tasks: set[asyncio.Task] = set() | ||
|
||
|
||
def create_background_task(coro: Coroutine) -> asyncio.Task: | ||
""" | ||
Create and track a task. | ||
Normally tasks are weak-referenced by asyncio. | ||
We keep track of them, so they can be completed before GC kicks in. | ||
""" | ||
task = asyncio.create_task(coro) | ||
_background_tasks.add(task) | ||
task.add_done_callback(_background_tasks.discard) | ||
return task | ||
|
||
|
||
def background_task(f: Callable[..., Coroutine]): | ||
""" | ||
Wraps an async function to be run using ``create_background_task``. | ||
Useful to use async functions like signal handlers or GTK template callbacks. | ||
Note: The return value will be lost, so this is not suitable when you need to | ||
return something from the coroutine, what might be needed in some signal handlers. | ||
""" | ||
|
||
@functools.wraps(f) | ||
def decor(*args, **kwargs): | ||
create_background_task(f(*args, **kwargs)) | ||
|
||
return decor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.