Skip to content

Commit 6205a6c

Browse files
rafaelmardojaimufeedali
authored andcommitted
refactor(asyncio): Update to use new PyGObject APIs and recomendations
1 parent 975046d commit 6205a6c

File tree

3 files changed

+16
-49
lines changed

3 files changed

+16
-49
lines changed

dialect/asyncio.py

+4-32
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
1-
import asyncio
2-
import contextlib
31
import functools
42
from typing import Callable, Coroutine
53

6-
from gi.events import GLibEventLoopPolicy
7-
8-
9-
@contextlib.contextmanager
10-
def glib_event_loop_policy():
11-
original = asyncio.get_event_loop_policy()
12-
policy = GLibEventLoopPolicy()
13-
asyncio.set_event_loop_policy(policy)
14-
try:
15-
yield policy
16-
finally:
17-
asyncio.set_event_loop_policy(original)
18-
19-
20-
_background_tasks: set[asyncio.Task] = set()
21-
22-
23-
def create_background_task(coro: Coroutine) -> asyncio.Task:
24-
"""
25-
Create and track a task.
26-
27-
Normally tasks are weak-referenced by asyncio.
28-
We keep track of them, so they can be completed before GC kicks in.
29-
"""
30-
task = asyncio.create_task(coro)
31-
_background_tasks.add(task)
32-
task.add_done_callback(_background_tasks.discard)
33-
return task
4+
from gi.repository import Gio
345

356

367
def background_task(f: Callable[..., Coroutine]):
378
"""
38-
Wraps an async function to be run using ``create_background_task``.
9+
Wraps an async function to be run using ``Gio.Application.create_asyncio_task``.
3910
4011
Useful to use async functions like signal handlers or GTK template callbacks.
4112
@@ -45,6 +16,7 @@ def background_task(f: Callable[..., Coroutine]):
4516

4617
@functools.wraps(f)
4718
def decor(*args, **kwargs):
48-
create_background_task(f(*args, **kwargs))
19+
app = Gio.Application.get_default()
20+
app.create_asyncio_task(f(*args, **kwargs))
4921

5022
return decor

dialect/main.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
# SPDX-License-Identifier: GPL-3.0-or-later
55

66
# Initial setup
7+
import asyncio
78
import logging
89
import sys
910

1011
import gi
12+
from gi.events import GLibEventLoopPolicy
1113

1214
try:
1315
gi.require_version("Gdk", "4.0")
@@ -23,7 +25,6 @@
2325
except ImportError or ValueError:
2426
logging.error("Error: GObject dependencies not met.")
2527

26-
from dialect.asyncio import glib_event_loop_policy
2728
from dialect.define import APP_ID, RES_PATH, VERSION
2829
from dialect.preferences import DialectPreferencesDialog
2930
from dialect.settings import Settings
@@ -196,11 +197,8 @@ def _on_quit(self, _action, _param):
196197

197198

198199
def main():
200+
# Set the asyncio event loop policy from PyGObject
201+
asyncio.set_event_loop_policy(GLibEventLoopPolicy())
199202
# Run the Application
200203
app = Dialect()
201-
exit_code = 0
202-
203-
with glib_event_loop_policy():
204-
exit_code = app.run(sys.argv)
205-
206-
return exit_code
204+
return app.run(sys.argv)

dialect/widgets/provider_preferences.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from gi.repository import Adw, GObject, Gtk
1212

13-
from dialect.asyncio import create_background_task
13+
from dialect.asyncio import background_task
1414
from dialect.define import RES_PATH
1515
from dialect.providers import ProviderCapability, RequestError
1616

@@ -74,8 +74,9 @@ def _check_settings(self):
7474

7575
self.api_usage_group.props.visible = False
7676
if self.provider.supports_api_usage:
77-
create_background_task(self._load_api_usage())
77+
self._load_api_usage()
7878

79+
@background_task
7980
async def _load_api_usage(self):
8081
if not self.provider:
8182
return
@@ -92,11 +93,9 @@ async def _load_api_usage(self):
9293
logging.error(exc)
9394

9495
@Gtk.Template.Callback()
95-
def _on_instance_apply(self, _row):
96+
@background_task
97+
async def _on_instance_apply(self, _row):
9698
"""Called on self.instance_entry::apply signal"""
97-
create_background_task(self._instance_apply())
98-
99-
async def _instance_apply(self):
10099
if not self.provider:
101100
return
102101

@@ -160,11 +159,9 @@ def _on_reset_instance(self, _button):
160159
self.instance_entry.props.text = self.provider.instance_url
161160

162161
@Gtk.Template.Callback()
163-
def _on_api_key_apply(self, _row):
162+
@background_task
163+
async def _on_api_key_apply(self, _row):
164164
"""Called on self.api_key_entry::apply signal"""
165-
create_background_task(self._api_key_apply())
166-
167-
async def _api_key_apply(self):
168165
if not self.provider:
169166
return
170167

0 commit comments

Comments
 (0)