got the error "got Future <Future pending> attached to a different loop" #633
-
hello, what i did :
then i got the error as the title . maybe i don't need to using gather again as the asyncssh.run already used? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
I'd need to see something closer to the actual code to help diagnose this. For instance, you don't list what code you are using to opening the connection, and whether or not you are calling await on that, which you need to do. If you want even the connect operation to happen in parallel, you could do something similar on those connections of using Also, you should avoid creating multiple event loops here. There should be exactly one, ideally created as part of a call to |
Beta Was this translation helpful? Give feedback.
-
Sorry, this is a bit hard to follow since you seem to be driving everything through pytest. Do you have a version of it that shows what it would look like under regular execution, and does that have the same problem? I'm particularly concerned with the way you are calling asyncio.get_event_loop() in your basic_env() setup, as that seems like it has the potential to create multiple event loops, at least in the case where there are multiple test functions being called, and I don't know how that will interact with an event loop set up by pytest when you use @pytest.mark.asyncio. I think it would be better to let pytest worry about getting the event loop set up and not requesting one yourself. |
Beta Was this translation helpful? Give feedback.
-
example.zip |
Beta Was this translation helpful? Give feedback.
-
You are still creating multiple event loops here, since you are calling if __name__ == '__main__':
driver_ssh = BCSSHDriver()
asyncio.run(server_connection(driver_ssh))
asyncio.run(run_command(driver_ssh)) to: async def main():
driver_ssh = BCSSHDriver()
await server_connection(driver_ssh)
await run_command(driver_ssh)
if __name__ == '__main__':
asyncio.run(main()) The alternative would be to create an event loop and call |
Beta Was this translation helpful? Give feedback.
-
Glad to hear that it worked! If pytest-asyncio is creating a new loop for each test case and you had separate test cases for the creation of the connections vs. the running of commands, you'd see the same problem you did here. To fix that, you'd need to combine these into a single test case. It's ok to have multiple test cases, but only if they don't share any AsyncSSH objects between them. Each time you start a new event loop, you'd need to create a fresh set of AsyncSSH objects (or any other asyncio objects). You might be able to control this with the scope option. See more info on this at https://pytest-asyncio.readthedocs.io/en/latest/reference/decorators/index.html. In particular, note the part about needing to increase the scope on the event_loop fixture if you increase it elsewhere. |
Beta Was this translation helpful? Give feedback.
You are still creating multiple event loops here, since you are calling
asyncio.run()
multiple times. You can't use any AsyncSSH objects created in the first call toasyncio.run()
after that call returns, as the associated event loop stored inside the AsyncSSH objects will be closed at that point. Try changing from:to:
The alternative would be to create an event loop and c…