Skip to content

Run APScheduler coroutine jobs on a portal loop - #270

Open
ricardo-agz wants to merge 1 commit into
mainfrom
ricardo/apscheduler-async-jobs
Open

Run APScheduler coroutine jobs on a portal loop#270
ricardo-agz wants to merge 1 commit into
mainfrom
ricardo/apscheduler-async-jobs

Conversation

@ricardo-agz

Copy link
Copy Markdown
Collaborator

Fixes coroutine jobs never executing on Vercel: the inline executor rejected them whenever an event loop was already running, and every queue worker delivery runs on one (vercel-queue invokes sync subscription handlers on its event loop thread).

Coroutine jobs now run on a process-lifetime AnyIO blocking portal. One persistent loop rather than a loop per run also means loop-bound resources a job creates (async Redis clients, locks) stay valid across runs, which is the same contract a stock AsyncIOScheduler gives jobs by running them all on its loop.

The sync-context path changes accordingly: previously each run got a fresh asyncio.run loop, which invalidated those same resources between runs.

Found by a stock FastAPI + AsyncIOScheduler demo whose async def job failed on every delivery with "cannot run APScheduler async jobs from a running event loop".

The inline executor rejected coroutine jobs whenever a loop was already
running, which is every queue worker delivery: vercel-queue invokes sync
subscription handlers on its own event loop thread, so async jobs on a
stock AsyncIOScheduler never executed on Vercel.

Run them on a process-lifetime AnyIO blocking portal instead. A single
persistent loop also replaces the per-call asyncio.run of the sync path,
so loop-bound resources a job creates (async clients, locks) remain
valid between runs, matching what a stock AsyncIOScheduler provides.
@vercel

vercel Bot commented Aug 8, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
vercel-py Ready Ready Preview Aug 8, 2026 7:11am

Request Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant