-
Notifications
You must be signed in to change notification settings - Fork 149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RuntimeError: Cannot run the event loop while another loop is running using pytest.main #374
Comments
@PeterStolz thank you for the separate issue report. Sorry to cause you extra work, but it's much easier to keep on top of existing problems and to follow discussions.
Interestingly, I do not get an error when I run your example. Note that I did not uncomment any lines. The test finishes successfully:
I see your pytest-asyncio version is |
I was using pytest-asyncio master to verify that the Issue still exists. I ran it again on the latest pypi version (asyncio-0.18.3 and python 3.10.4) and it still crashes. Did you really run the |
That is commendable, thank you :)
No, I ran Why does |
In this simple proof of concept there is no need to call |
To give you a minimal proof of concept in python3.10:
runner.py
test_file.py
The cause for this exception is that my setup already has an existing running EventLoop and in that loop I invoke
pytest.main
.pytest.asyncio
useswrap_in_sync
in the async fixtures and tests, which callspytest-asyncio/pytest_asyncio/plugin.py
Line 454 in 860ff51
Therefore calling
run_until_complete
within a coroutine raises an exception.Code executed inside an EventLoop can't stop the current EvenntLoop, create a new loop, run the code in that loop and restore the old loop. At least not without heavy modification. Python developers also stated that this behavior will not be changed.
This can be mitigated by using nest_asyncio, however there are different eventloop implementations that are not compatible with nest_asyncio. (My project used fastapi which uses a different loop implementation)
If there is an async entrypoiny like
pytest.async_main
into pytest that may be used as well. If we make all functions awaitable, we would not need to use run_until_complete, but could simply useawait
. However I don't think that this is feasible.In the end I just ran pytest in a subprocess to call it outside of a coroutine.
As requested I further elaborated on my problem from #359 (comment)
The text was updated successfully, but these errors were encountered: