Re-using existing event loop in BlockingPortal
/ compatibility with pytest-asyncio
#883
Unanswered
tuukkamustonen
asked this question in
Q&A
Replies: 1 comment 1 reply
-
No, it's not possible to pass an existing event loop to it. When you create and enter a |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is it possible to pass existing event-loop to
BlockingPortal
?Starlette/FastAPI
TestClient
uses blocking portal underneath, and as I've understood it spawns a thread and runs an event loop of its own there.So if you use something like
pytest-asyncio
which creates the event loop, initialize an async resource in an async test or fixture, then pass that it the app, it gets run in a different event loop and might raise and error.As an example,
psycopg
doesn't raise error, butasyncpg
does. See sqlalchemy/sqlalchemy#12211 (reply in thread). In general, switching the event loop is probably not a good idea / guaranteed to work, if the resource maintains state like connections etc.So... is it possible to use
BlockingPortal
with existing (asyncio
) event loop (or should I overwrite theTestClient
to use something likeloop.run_until_complete()
instead)?Beta Was this translation helpful? Give feedback.
All reactions