Skip to content

Commit 03a27d4

Browse files
committed
Add wrapper to asyncio.to_thread
1 parent 2a30a33 commit 03a27d4

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

qubesadmin/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
from __future__ import annotations
2828

29+
import asyncio
2930
import fcntl
31+
import functools
3032
import os
3133
import re
3234
import typing
@@ -256,3 +258,18 @@ def qbool(value: str | int | bool) -> bool:
256258
)
257259

258260
return bool(value)
261+
262+
263+
async def async_thread(func, /, *args, **kwargs):
264+
"""
265+
Asynchronously run function in a separate thread.
266+
267+
:param func: synchronous function
268+
:param args: args to pass to function
269+
:param kwargs: kwargs to pass to function
270+
:return: function result
271+
272+
>>> from qubesadmin.utils import async_thread
273+
>>> await async_thread(vm.shutdown, force=True, wait=True)
274+
"""
275+
return await asyncio.to_thread(functools.partial(func, *args, **kwargs))

0 commit comments

Comments
 (0)