Skip to content

Commit 904a264

Browse files
committed
Merge remote-tracking branch 'origin/pr/820'
* origin/pr/820: Fix mypy error my creating a Task Short loop.run_in_executor with asyncio.to_thread Pull request description: Doesn't require: - finding the loop as it uses the default; - passing "None" as executor as it uses the default; - using partial to pass kwargs --- ~Didn't test at all~, but would be nice to use a shorter version, since we are not using the full capabilities of the executor, a wrapper is easier to read. If you look at `asyncio/threads.py`, it's just calling `loop.run_in_executor`. Edit: tested manually the preloaded disposable early request before qmemman finishes and watching the logs. Didn't run integration tests, and that would be appropriate for the different files I changed.
2 parents 9d83ea2 + 5d1ca12 commit 904a264

7 files changed

Lines changed: 16 additions & 28 deletions

File tree

qubes/storage/reflink.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ def _coroutinized(function):
6060

6161
@functools.wraps(function)
6262
async def wrapper(*args, **kwargs):
63-
return await asyncio.get_event_loop().run_in_executor(
64-
None, functools.partial(function, *args, **kwargs)
65-
)
63+
return await asyncio.to_thread(function, *args, **kwargs)
6664

6765
return wrapper
6866

qubes/storage/zfs.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,11 +2044,7 @@ async def _wipe_and_clone_from(self, source: qubes.storage.Volume) -> None:
20442044
self.log.debug("Source is a File volume")
20452045
# File volume export() does not actually return a coroutine.
20462046
# This isn't just a typing error. The await() fails.
2047-
loop = asyncio.get_event_loop()
2048-
in_ = await loop.run_in_executor(
2049-
None,
2050-
source.export,
2051-
) # type:ignore
2047+
in_ = await asyncio.to_thread(source.export) # type:ignore
20522048
else:
20532049
self.log.debug("Source is not a ZFS volume")
20542050
in_ = await source.export() # type:ignore

qubes/tests/integ/backup.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,11 @@ def restore_backup(
250250
client_app = qubesadmin.Qubes()
251251
if appvm:
252252
appvm = self.loop.run_until_complete(
253-
self.loop.run_in_executor(
254-
None, client_app.domains.__getitem__, appvm.name
255-
)
253+
asyncio.to_thread(client_app.domains.__getitem__, appvm.name)
256254
)
257255
with self.assertNotRaises(qubesadmin.exc.QubesException):
258256
restore_op = self.loop.run_until_complete(
259-
self.loop.run_in_executor(
260-
None,
257+
asyncio.to_thread(
261258
qubesadmin.backup.restore.BackupRestore,
262259
client_app,
263260
backupfile,
@@ -269,17 +266,15 @@ def restore_backup(
269266
for key, value in options.items():
270267
setattr(restore_op.options, key, value)
271268
restore_info = self.loop.run_until_complete(
272-
self.loop.run_in_executor(None, restore_op.get_restore_info)
269+
asyncio.to_thread(restore_op.get_restore_info)
273270
)
274271
if callable(manipulate_restore_info):
275272
restore_info = manipulate_restore_info(restore_info)
276273
self.log.debug(restore_op.get_restore_summary(restore_info))
277274

278275
with self.assertNotRaises(qubesadmin.exc.QubesException):
279276
self.loop.run_until_complete(
280-
self.loop.run_in_executor(
281-
None, restore_op.restore_do, restore_info
282-
)
277+
asyncio.to_thread(restore_op.restore_do, restore_info)
283278
)
284279

285280
errors = []

qubes/tests/integ/backupdispvm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def restore_backup(
100100
restore_in_dispvm = RestoreInDisposableVM(args.app, args)
101101
try:
102102
backup_log = self.loop.run_until_complete(
103-
self.loop.run_in_executor(None, restore_in_dispvm.run)
103+
asyncio.to_thread(restore_in_dispvm.run)
104104
)
105105
except qubesadmin.exc.BackupRestoreError as e:
106106
self.fail(str(e) + " backup log: " + e.backup_log.decode())

qubes/tests/integ/vm_qrexec_gui.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,7 @@ async def _test_300_bug_1028_gui_memory_pinning(self):
812812
winid = await self.wait_for_window_coro(
813813
self.testvm1.name + ":xterm", search_class=True
814814
)
815-
xprop = await asyncio.get_event_loop().run_in_executor(
816-
None,
815+
xprop = await asyncio.to_thread(
817816
subprocess.check_output,
818817
["xprop", "-notype", "-id", winid, "_QUBES_VMWINDOWID"],
819818
)
@@ -850,8 +849,7 @@ async def _test_300_bug_1028_gui_memory_pinning(self):
850849
"gm import -window {} rgba:-".format(vm_winid)
851850
)
852851

853-
dom0_image = await asyncio.get_event_loop().run_in_executor(
854-
None,
852+
dom0_image = await asyncio.to_thread(
855853
subprocess.check_output,
856854
["gm", "import", "-window", winid, "rgba:-"],
857855
)

qubes/vm/dispvm.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,10 +626,11 @@ async def on_domain_pre_paused(self, event, **kwargs) -> None:
626626
return
627627
break_task = asyncio.create_task(self.preload_requested_event.wait())
628628
qmemman_client = qubes.qmemman.client.QMemmanClient()
629-
qmemman_task = asyncio.get_running_loop().run_in_executor(
630-
None,
631-
qmemman_client.set_mem, # type: ignore[arg-type]
632-
{self.xid: 0},
629+
qmemman_task = asyncio.create_task(
630+
asyncio.to_thread(
631+
qmemman_client.set_mem,
632+
{self.xid: 0},
633+
)
633634
)
634635
tasks: list = [break_task, qmemman_task]
635636
result = None

qubes/vm/qubesvm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,8 +1453,8 @@ async def start(
14531453
notify_function=notify_function,
14541454
)
14551455

1456-
qmemman_client = await asyncio.get_event_loop().run_in_executor(
1457-
None, self.request_mem, mem_required
1456+
qmemman_client = await asyncio.to_thread(
1457+
self.request_mem, mem_required
14581458
)
14591459

14601460
await self.storage.start()

0 commit comments

Comments
 (0)