I'm having a hard time understanding how the AsyncIOScheduler works, and how is it non blocking?
If my job is executing a blocking function, will the AsyncIOScheduler
be blocking?
And what if I use AsyncIOScheduler
with ThreadPoolExecutor
? How does that work? Can I await the job execution?
So, in APScheduler there are 3 important components:
For this question, only 1 and 2 are relevant.
The Scheduler is simply who decides when to call the jobs based on their interval settings, in the case of AsyncIOScheduler
it uses asyncio to make the waiting period non blocking. It runs in the same process and thread as the main. It is very useful if your application is already running on an asyncio loop since it saves the overhead of running a new proccess/thread.
Now, when a job needs to be executed, is the Executor who is called, in the case of AsyncIOScheduler
, by default is uses AsyncIOExecutor
, which runs in the same thread and process as the Scheduler IF the job function is signed as async, else it uses asyncio's run_in_executor which runs it in a thread pool.
Which brings us to the last question, what happens if we use AsyncIOScheduler
with ThreadPoolExecutor
? Well, technically is the same as using the default Executor with a non async function, it will run it in a thread pool, but the scheduler will remain in the main thread.