]> git.ipfire.org Git - pbs.git/commitdiff
decorators: Create a decorator to run something in a thread
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 9 Feb 2025 13:24:31 +0000 (13:24 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 9 Feb 2025 13:24:31 +0000 (13:24 +0000)
This is just for convenience and to write less code since we are using
this pattern a lot.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/decorators.py

index d6d948e7fa6082faa4372fdd10bedd275a5cf472..df04425b1cbcb9167a2f0b26c88552ad17b40f28 100644 (file)
@@ -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