From 5104270c312229be1eaa37887f77c04c09b54f7a Mon Sep 17 00:00:00 2001 From: dilek <128893136+dilekk1@users.noreply.github.com> Date: Sat, 2 Nov 2024 16:34:43 +0300 Subject: [PATCH] Create awaitme_dilek_celebi.py --- Week05/awaitme_dilek_celebi.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Week05/awaitme_dilek_celebi.py diff --git a/Week05/awaitme_dilek_celebi.py b/Week05/awaitme_dilek_celebi.py new file mode 100644 index 00000000..520ac512 --- /dev/null +++ b/Week05/awaitme_dilek_celebi.py @@ -0,0 +1,17 @@ +import asyncio +from functools import wraps + +def awaitme(func): + """ + A decorator that turns any function into a coroutine. + It will pass all arguments to the function properly and return any value from the function. + """ + @wraps(func) + async def wrapper(*args, **kwargs): + # Run the function in a separate thread if it's not a coroutine + if not asyncio.iscoroutinefunction(func): + loop = asyncio.get_event_loop() + return await loop.run_in_executor(None, func, *args, **kwargs) + # If it's already a coroutine, simply await it + return await func(*args, **kwargs) + return wrapper