From 87a7aefcf3262c23fa53fd083f7aacf5523d5ab3 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Sat, 14 Jan 2023 09:23:03 +1300 Subject: [PATCH] examples: switch to newer asyncio API Available since Python 3.7. Signed-off-by: Julian Oes --- examples/calibration.py | 4 ++-- examples/camera.py | 4 ++-- examples/camera_params.py | 4 ++-- examples/failure_injection.py | 4 ++-- examples/firmware_version.py | 4 ++-- examples/follow_me_example.py | 4 ++-- examples/geofence.py | 6 +++--- examples/gimbal.py | 5 ++--- examples/goto.py | 4 ++-- examples/logfile_download.py | 3 ++- examples/manual_control.py | 5 ++--- examples/mission.py | 4 ++-- examples/mission_import.py | 4 ++-- examples/offboard_attitude.py | 4 ++-- examples/offboard_position_ned.py | 4 ++-- examples/offboard_position_velocity_ned.py | 4 ++-- examples/offboard_velocity_body.py | 4 ++-- examples/offboard_velocity_ned.py | 4 ++-- examples/send_status_message.py | 4 ++-- examples/takeoff_and_land.py | 4 ++-- examples/takeoff_and_land_keyboard_control.py | 5 ++--- examples/telemetry.py | 7 ++----- examples/telemetry_flight_mode.py | 4 ++-- examples/telemetry_is_armed_is_in_air.py | 7 ++----- examples/telemetry_takeoff_and_land.py | 3 ++- examples/transponder.py | 9 +++------ examples/tune.py | 4 ++-- 27 files changed, 56 insertions(+), 66 deletions(-) mode change 100644 => 100755 examples/takeoff_and_land_keyboard_control.py diff --git a/examples/calibration.py b/examples/calibration.py index 133e4988..eb8bb417 100755 --- a/examples/calibration.py +++ b/examples/calibration.py @@ -31,5 +31,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/camera.py b/examples/camera.py index e39f0981..c027f36d 100755 --- a/examples/camera.py +++ b/examples/camera.py @@ -56,5 +56,5 @@ async def print_status(drone): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/camera_params.py b/examples/camera_params.py index d3c0a3b1..007a6420 100755 --- a/examples/camera_params.py +++ b/examples/camera_params.py @@ -185,5 +185,5 @@ async def make_user_choose_option_range(possible_options): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/failure_injection.py b/examples/failure_injection.py index d3437b56..036f252c 100755 --- a/examples/failure_injection.py +++ b/examples/failure_injection.py @@ -59,5 +59,5 @@ async def run(): await drone.param.set_param_int('SYS_FAILURE_EN', 0) if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/firmware_version.py b/examples/firmware_version.py index 63bc2d43..290b0d30 100755 --- a/examples/firmware_version.py +++ b/examples/firmware_version.py @@ -19,5 +19,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/follow_me_example.py b/examples/follow_me_example.py index da80bd6e..735ad732 100755 --- a/examples/follow_me_example.py +++ b/examples/follow_me_example.py @@ -64,5 +64,5 @@ async def fly_drone(): await drone.action.land() if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(fly_drone()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/geofence.py b/examples/geofence.py index e8a28209..96422fcb 100755 --- a/examples/geofence.py +++ b/examples/geofence.py @@ -7,7 +7,7 @@ """ This example shows how to use the geofence plugin. -Note: The behavior when your vehicle hits the geofence is NOT configured in this example. +Note: The behavior when your vehicle hits the geofence is NOT configured in this example. """ @@ -51,5 +51,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/gimbal.py b/examples/gimbal.py index a5c67c94..f278dc0b 100755 --- a/examples/gimbal.py +++ b/examples/gimbal.py @@ -83,6 +83,5 @@ async def print_gimbal_position(drone): if __name__ == "__main__": - # Start the main function - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/goto.py b/examples/goto.py index 198f255a..01e3fc26 100755 --- a/examples/goto.py +++ b/examples/goto.py @@ -43,5 +43,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/logfile_download.py b/examples/logfile_download.py index 71af0ff7..a6b833ab 100755 --- a/examples/logfile_download.py +++ b/examples/logfile_download.py @@ -42,4 +42,5 @@ async def get_entries(drone): if __name__ == "__main__": - asyncio.get_event_loop().run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/manual_control.py b/examples/manual_control.py index a0ed64b7..a3d86766 100755 --- a/examples/manual_control.py +++ b/examples/manual_control.py @@ -91,6 +91,5 @@ async def manual_controls(): if __name__ == "__main__": - - loop = asyncio.get_event_loop() - loop.run_until_complete(manual_controls()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/mission.py b/examples/mission.py index 93f0affd..07d5de63 100755 --- a/examples/mission.py +++ b/examples/mission.py @@ -116,5 +116,5 @@ async def observe_is_in_air(drone, running_tasks): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/mission_import.py b/examples/mission_import.py index 2b6f2dd2..225f6c6e 100755 --- a/examples/mission_import.py +++ b/examples/mission_import.py @@ -24,5 +24,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/offboard_attitude.py b/examples/offboard_attitude.py index 4ecbf13b..27e22024 100755 --- a/examples/offboard_attitude.py +++ b/examples/offboard_attitude.py @@ -72,5 +72,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/offboard_position_ned.py b/examples/offboard_position_ned.py index 5c6dedb7..eeb478f9 100755 --- a/examples/offboard_position_ned.py +++ b/examples/offboard_position_ned.py @@ -71,5 +71,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/offboard_position_velocity_ned.py b/examples/offboard_position_velocity_ned.py index d8513687..f093c95e 100755 --- a/examples/offboard_position_velocity_ned.py +++ b/examples/offboard_position_velocity_ned.py @@ -64,5 +64,5 @@ async def print_z_velocity(drone): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/offboard_velocity_body.py b/examples/offboard_velocity_body.py index aa6562b0..b4886ec5 100755 --- a/examples/offboard_velocity_body.py +++ b/examples/offboard_velocity_body.py @@ -86,5 +86,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/offboard_velocity_ned.py b/examples/offboard_velocity_ned.py index e8e70884..716af9df 100755 --- a/examples/offboard_velocity_ned.py +++ b/examples/offboard_velocity_ned.py @@ -79,5 +79,5 @@ async def run(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/send_status_message.py b/examples/send_status_message.py index 0d4a3546..ddbca512 100755 --- a/examples/send_status_message.py +++ b/examples/send_status_message.py @@ -29,5 +29,5 @@ async def run(): break if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/takeoff_and_land.py b/examples/takeoff_and_land.py index a5922c39..666897f5 100755 --- a/examples/takeoff_and_land.py +++ b/examples/takeoff_and_land.py @@ -47,5 +47,5 @@ async def print_status_text(drone): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/takeoff_and_land_keyboard_control.py b/examples/takeoff_and_land_keyboard_control.py old mode 100644 new mode 100755 index 5e9787f6..6561222a --- a/examples/takeoff_and_land_keyboard_control.py +++ b/examples/takeoff_and_land_keyboard_control.py @@ -1,6 +1,4 @@ -""" -Author: @woosal1337 -""" +#!/usr/bin/env python3 import asyncio import pygame @@ -148,6 +146,7 @@ async def print_position(drone=drone): if __name__ == "__main__": + # TODO: use new asyncio methods loop = asyncio.get_event_loop() loop.run_until_complete(setup()) loop.run_until_complete(main()) diff --git a/examples/telemetry.py b/examples/telemetry.py index ab632696..e85bd351 100755 --- a/examples/telemetry.py +++ b/examples/telemetry.py @@ -36,8 +36,5 @@ async def print_position(drone): if __name__ == "__main__": - # Start the main function - asyncio.ensure_future(run()) - - # Runs the event loop until the program is canceled with e.g. CTRL-C - asyncio.get_event_loop().run_forever() + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/telemetry_flight_mode.py b/examples/telemetry_flight_mode.py index 3159c759..05732bab 100755 --- a/examples/telemetry_flight_mode.py +++ b/examples/telemetry_flight_mode.py @@ -19,5 +19,5 @@ async def print_flight_mode(): if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(print_flight_mode()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/telemetry_is_armed_is_in_air.py b/examples/telemetry_is_armed_is_in_air.py index 73842629..283f941c 100755 --- a/examples/telemetry_is_armed_is_in_air.py +++ b/examples/telemetry_is_armed_is_in_air.py @@ -23,8 +23,5 @@ async def print_is_in_air(drone): if __name__ == "__main__": - # Start the main function - asyncio.ensure_future(run()) - - # Runs the event loop until the program is canceled with e.g. CTRL-C - asyncio.get_event_loop().run_forever() + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/telemetry_takeoff_and_land.py b/examples/telemetry_takeoff_and_land.py index 081a7958..caf5015d 100755 --- a/examples/telemetry_takeoff_and_land.py +++ b/examples/telemetry_takeoff_and_land.py @@ -108,4 +108,5 @@ async def observe_is_in_air(drone, running_tasks): if __name__ == "__main__": - asyncio.get_event_loop().run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/transponder.py b/examples/transponder.py index 194453fc..1af8201b 100644 --- a/examples/transponder.py +++ b/examples/transponder.py @@ -9,7 +9,7 @@ async def run(): drone = System() #await drone.connect(system_address="udp://:14540") await drone.connect() - + # Start the tasks asyncio.ensure_future(print_transponders(drone)) @@ -19,8 +19,5 @@ async def print_transponders(drone): if __name__ == "__main__": - # Start the main function - asyncio.ensure_future(run()) - - # Runs the event loop until the program is canceled with e.g. CTRL-C - asyncio.get_event_loop().run_forever() + # Run the asyncio loop + asyncio.run(run()) diff --git a/examples/tune.py b/examples/tune.py index 6989d03b..7dc3a984 100755 --- a/examples/tune.py +++ b/examples/tune.py @@ -64,5 +64,5 @@ async def run(): print("Tune played") if __name__ == "__main__": - loop = asyncio.get_event_loop() - loop.run_until_complete(run()) + # Run the asyncio loop + asyncio.run(run())