-
-
Notifications
You must be signed in to change notification settings - Fork 51
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
FastAPI integration Async Redis session dependency override, error during test RuntimeError: <Queue > is bound to a different event loop #292
Comments
Can you post the full stack trace? I am not sure, but I think you don't need to include the event_loop when defining the redis fixture. @pytest.fixture
async def redis():
redis_session = fakeredis.FakeAsyncRedis(decode_responses=True, encoding="utf-8", )
async def _get_async_redis_session() -> AsyncIterator[fakeredis.FakeAsyncRedis]:
yield redis_session
for app in all_fast_apps:
app.dependency_overrides[get_async_redis_session] = _get_async_redis_session
return redis_session You can also see how the fixture is defined in the tests for this package (remove the logic for |
I don't think this is related to FakeRedis.. See this thread: I suspect you are creating your FastAPI server and the fakeredis connection on different event-loops |
Here you are creating a new event-loop: @pytest.fixture(scope="session")
def event_loop():
policy = asyncio.get_event_loop_policy()
loop = policy.new_event_loop()
yield loop
loop.close() |
Please update here. |
Indeed I have also hit this problem. I couldn't figure out how to get it all working. You'd think that Anyway in the end I worked around it using |
Hi Steve, do you mind publishing the snippet of your solution? it might be useful for other people (and for me :) ) |
@christiansicari there's a bit of redaction here since it's proprietary code, and I haven't included all the imports, but it's a bit like this:
In Then I have a bunch of async tests that use the It might be that the code here is obvious, and my key insight was to give up on trying to write synchronous tests. The code you posted has |
I should probably warn that this code is a bit scrappy. It doesn't bother undoing the modification to |
@steve-mavens do you mind adding it in the docs? around here would probably be good. |
I can add something about how to intersect fastapi and async fakeredis :-) I'm making some assumptions about how the fastapi app is set up, and I don't know how typical my code is. I'm not deeply involved in the fastapi community to know what's most common: I just got the dependencies I needed working and then mostly forgot about them. |
Do you mind reviewing this section in the documentation and see whether it makes sense. If so, close this issue please |
I don't think it's complete. The code you've used doesn't explain what |
Well, I'll keep this open, if any of you can spend some time to improve the docs it would be great. I haven't worked with FastAPI so I am not in a position to write it. |
I've just updated one of my projects to the latest pytest-asyncio (released March 19th), and at least for my usage the need to have a session-scoped event loop has gone away. I can just remove it and I don't get the "bound to a different event loop" errors any more. The reason I even made the experiment of removing it is that pytest-asyncio has deprecated user-redefining the It might be that this is only fixed because all my other fixtures are function-scoped as well, but as a brief experiment I did also get the tests working by setting some of my fixtures to @christiansicari would it be possible for you to check this for your usage? That is:
Then see whether the event loop errors have gone. Hopefully tomorrow or the day after I will finish everything I'm doing on this project I've changed today, and move on to my project from which I took the code snippet above. Then I can test the fastapi dependency override for myself. |
@cunla I have created a standalone FastAPI app that uses Redis, and written a test for it to demonstrate the dependency override. Then I've copied that code into your docs and the PR is [edit: 311, which I have closed] This does not need the eventloop fixture like in @christiansicari 's original question. I believe this to be due to improvements in pytest-asyncio since this question was asked, since my project used to need the same thing and no longer does. |
Something is very wrong with that PR. For me the page now just shows a massive flickering discord URL. I have no idea how, and I'm now going to check from another machine. Could be some kind of compromise. Anyway, here is an alternative PR that should be equivalent: #312 |
The issue is that a user named "simulified" posted a comment to the PR with content that (when Javascript is enabled) replaces the whole content of the page with an image. I've reported the user to github, but I don't know if that will have any effect. I have also blocked the user, although that doesn't prevent their comments appearing on this repo's PRs. When I load the page with javascript disabled, I can see the malicious comment, but github has decided in its wisdom that the "..." menu on comments should only work with Javascript enabled. So, I can't even try to delete the comment. I don't believe it would work anyway, since I think I need to have write access to the repo to delete comments even on PRs I raised. The other PR is OK for now, but this feels like the kind of exploit that could hit every repo on github within hours until they deal with it... |
Thanks for working on this. I'll review the PR later today. |
Describe the bug
I am currently developing a FastAPI app that gets a Redis Session from a Dependency Injection, and uses it to write some data on Redis.
I need to test the endpoint, therefore I created a pytext fixture which in turn:
in the test basically I call the endpoint, and I read the key I expect to get set, but I get the following error
test_redis - RuntimeError: <Queue at 0x111538a10 maxsize=0 tasks=1> is bound to a different event loop
To Reproduce
My fixtures are:
This is my dependency used to fetch a Redis Connection:
and this is the endpoint definition:
Finally the test where I get the error:
Expected behavior
Get the key set before in the endpoint
Desktop (please complete the following information):
Upvote & Fund
The text was updated successfully, but these errors were encountered: