You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RuntimeError: Task pending got Future attached to a different loop during Offboard control with FastAPI and MAVSDK
Description
I am working on a Python application that integrates FastAPI for web-based control and MAVSDK for controlling a PX4 drone. The application handles video processing with OpenCV and allows for user input to control drone tracking and segmentation. However, I encountered a RuntimeError related to asynchronous programming and event loop management when trying to send velocity commands to the drone in offboard mode. But when starting the mavsdk offboard commands from the terminal (instead of the web app) wi...
When attempting to send velocity commands to the drone in offboard mode, I encountered the following RuntimeError:
RuntimeError: Task <Task pending name='Task-1' coro=<main() running at c:\Users\Alireza\source\repos\PixEagle\src\main.py:68> cb=[_run_until_complete_cb() at C:\Users\Alireza\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py:184]> got Future <Future pending cb=[wrap_future.<locals>._call_check_cancel() at C:\Users\Alireza\source\repos\PixEagle\.venv\lib\site-packages\aiogrpc\utils.py:52]> attached to a different loop
ERROR:classes.px4_controller:Error updating telemetry: cannot schedule new futures after shutdown
This error indicates that the coroutine is trying to await a Future that is attached to a different event loop. This might be caused by the threading used to run the FastAPI server or some issue with the integration between FastAPI and MAVSDK's asyncio operations.
Steps Taken and Debugging Ideas
Unified Event Loop: Ensured that the main application and FastAPI server use the same event loop by starting the FastAPI server within the main event loop.
Task Management: Checked for any inconsistencies in task management and event loop usage in both main.py and px4_controller.py.
The follow command start/stop successfully from the app itself with key press activating. Suggesting that part is working. Only not working when running the same function (connect_px4) from the FastAPI endpoint.
Possible Problems
Threading Conflicts: Running the FastAPI server in a separate thread might be causing conflicts with the asyncio event loop used by MAVSDK.
Event Loop Handling: There might be issues with how the event loops are being managed, particularly with the telemetry update task in the PX4Controller.
Shutdown Sequence: The shutdown sequence might be prematurely stopping the event loop, causing new futures to be attached to a closed loop.
Request for Assistance
I would appreciate any recommendations or solutions to handle this situation effectively. Specifically:
Best practices for integrating FastAPI and MAVSDK to avoid event loop conflicts.
How to manage event loops and tasks in a multi-threaded environment like this.
Any example implementations or patterns.
Thank you for your assistance.
The text was updated successfully, but these errors were encountered:
Unified Event Loop: Ensured that the main application and FastAPI server use the same event loop by starting the FastAPI server within the main event loop.
That would have been my guess as well. You made sure that's the case?
I tried that.. and push to latest commit you can check that out
It works... but now it became so slow and laggy ... the video is no longer real time over http ... not applicable...
Do you think, is there a way we can have mavsdk and fastapi on different thread and run a command of mavsdk from fast api routes?
Unified Event Loop: Ensured that the main application and FastAPI server use the same event loop by starting the FastAPI server within the main event loop.
That would have been my guess as well. You made sure that's the case?
RuntimeError: Task pending got Future attached to a different loop during Offboard control with FastAPI and MAVSDK
Description
I am working on a Python application that integrates FastAPI for web-based control and MAVSDK for controlling a PX4 drone. The application handles video processing with OpenCV and allows for user input to control drone tracking and segmentation. However, I encountered a
RuntimeError
related to asynchronous programming and event loop management when trying to send velocity commands to the drone in offboard mode. But when starting the mavsdk offboard commands from the terminal (instead of the web app) wi...Repo URL: https://github.com/alireza787b/PixEagle
Implementation
main.py
:px4_controller.py
:Problem Encountered
When attempting to send velocity commands to the drone in offboard mode, I encountered the following
RuntimeError
:This error indicates that the coroutine is trying to await a Future that is attached to a different event loop. This might be caused by the threading used to run the FastAPI server or some issue with the integration between FastAPI and MAVSDK's asyncio operations.
Steps Taken and Debugging Ideas
main.py
andpx4_controller.py
.connect_px4
) from the FastAPI endpoint.Possible Problems
PX4Controller
.Request for Assistance
I would appreciate any recommendations or solutions to handle this situation effectively. Specifically:
Thank you for your assistance.
The text was updated successfully, but these errors were encountered: