From: Michael Tremer Date: Sun, 9 Feb 2025 13:24:31 +0000 (+0000) Subject: decorators: Create a decorator to run something in a thread X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=836a1ec03e0573e6429cb3d25012df3e77b5e207;p=pbs.git decorators: Create a decorator to run something in a thread This is just for convenience and to write less code since we are using this pattern a lot. Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/decorators.py b/src/buildservice/decorators.py index d6d948e7..df04425b 100644 --- a/src/buildservice/decorators.py +++ b/src/buildservice/decorators.py @@ -1,5 +1,18 @@ #!/usr/bin/python +import asyncio +import functools + +def run_in_thread(func): + """ + Decorator to run a synchronous function in a separate thread. + """ + @functools.wraps(func) + async def wrapper(*args, **kwargs): + return await asyncio.to_thread(func, *args, **kwargs) + + return wrapper + class lazy_property(property): """ The property is only computed once and then being