import asyncio import functools def to_sync_task(func): @functools.wraps(func) def wrapper(*args, **kwargs): try: loop = asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_forever() return loop.run_until_complete(func(*args, **kwargs)) return wrapper