import asyncio from functools import wraps def async_to_sync(func): @wraps(func) def wrapper(*args, **kwargs): # Get the current event loop loop = asyncio.get_event_loop() # If there is no current event loop, create a new one if loop.is_closed(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) # Run the async function until complete and return the result return loop.run_until_complete(func(*args, **kwargs)) return wrapper