Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Week05/awaitme_dilek_celebi.py
Original file line number Diff line number Diff line change
@@ -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