Open tekumara opened 1 year ago
A workaround for now is to use an async task, and to_thread from anyio to move sync methods to a thread, eg:
def sync_stuff():
print(asyncio.get_running_loop()) # will throw RuntimeError: no running event loop
... do actual sync stuff here ...
@app.task(hourly, execution="process")
async def do_daily() -> None:
await to_thread.run_sync(
sync_stuff
)
Hmm, ye. This is how the async loop is created:
There is only one place where the await keyword is actually needed and it is here where the execution function is called: https://github.com/Miksus/rocketry/blob/214933f1873a7ab148a235222285dd650c263e5c/rocketry/core/task.py#L596-L599
So in principle, we could fix this by having two sets of those methods that actually carry on running the task function: one would be sync and another would be async. Then before we create the loop we check if we really need to create one. However, there is a risk that the method gets duplicated and becomes hard to maintain. Probably needs more splitting,
This example might be useful to you.
Describe the bug
Tasks run on the async event loop even when
execution="process"
.To Reproduce
Output:
Some synchronously libraries assert there is no event loop and won't run, eg: Playwright in rocketry:
Expected behavior
When running a task in a process or thread there is no event loop.
Additional context
rocketry 2.5.1