Skip to content

Commit

Permalink
Merge pull request #183 from mavlink/fix-termination
Browse files Browse the repository at this point in the history
Fix tasks termination for Python 3.8
  • Loading branch information
julianoes authored May 12, 2020
2 parents 9ac7113 + 2413b10 commit 218169b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
19 changes: 13 additions & 6 deletions examples/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ async def run():
print(f"Drone discovered with UUID: {state.uuid}")
break

asyncio.ensure_future(print_camera_mode(drone))
asyncio.ensure_future(print_status(drone))
print_mode_task = asyncio.ensure_future(print_mode(drone))
print_status_task = asyncio.ensure_future(print_status(drone))
running_tasks = [print_mode_task, print_status_task]

print("Setting mode to 'PHOTO'")
try:
Expand All @@ -33,14 +34,20 @@ async def run():
except CameraError as error:
print(f"Couldn't take photo: {error._result.result}")

# Shut down the running coroutines (here 'print_camera_mode()' and
# Shut down the running coroutines (here 'print_mode()' and
# 'print_status()')
for task in running_tasks:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
await asyncio.get_event_loop().shutdown_asyncgens()


async def print_camera_mode(drone):
async for camera_mode in drone.camera.mode():
print(f"Camera mode: {camera_mode}")
async def print_mode(drone):
async for mode in drone.camera.mode():
print(f"Camera mode: {mode}")


async def print_status(drone):
Expand Down
15 changes: 12 additions & 3 deletions examples/mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ async def run():
print(f"Drone discovered with UUID: {state.uuid}")
break

asyncio.ensure_future(print_mission_progress(drone))
termination_task = asyncio.ensure_future(observe_is_in_air(drone))
print_mission_progress_task = asyncio.ensure_future(print_mission_progress(drone))

running_tasks = [print_mission_progress_task]
termination_task = asyncio.ensure_future(observe_is_in_air(drone, running_tasks))

mission_items = []
mission_items.append(MissionItem(47.398039859999997,
Expand Down Expand Up @@ -74,7 +76,7 @@ async def print_mission_progress(drone):
f"{mission_progress.total}")


async def observe_is_in_air(drone):
async def observe_is_in_air(drone, running_tasks):
""" Monitors whether the drone is flying or not and
returns after landing """

Expand All @@ -85,7 +87,14 @@ async def observe_is_in_air(drone):
was_in_air = is_in_air

if was_in_air and not is_in_air:
for task in running_tasks:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
await asyncio.get_event_loop().shutdown_asyncgens()

return


Expand Down
16 changes: 12 additions & 4 deletions examples/telemetry_takeoff_and_land.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ async def run():
break

# Start parallel tasks
asyncio.ensure_future(print_altitude(drone))
asyncio.ensure_future(print_flight_mode(drone))
termination_task = asyncio.ensure_future(observe_is_in_air(drone))
print_altitude_task = asyncio.ensure_future(print_altitude(drone))
print_flight_mode_task = asyncio.ensure_future(print_flight_mode(drone))

running_tasks = [print_altitude_task, print_flight_mode_task]
termination_task = asyncio.ensure_future(observe_is_in_air(drone, running_tasks))

# Execute the maneuvers
print("-- Arming")
Expand Down Expand Up @@ -78,7 +80,7 @@ async def print_flight_mode(drone):
print(f"Flight mode: {flight_mode}")


async def observe_is_in_air(drone):
async def observe_is_in_air(drone, running_tasks):
""" Monitors whether the drone is flying or not and
returns after landing """

Expand All @@ -89,6 +91,12 @@ async def observe_is_in_air(drone):
was_in_air = is_in_air

if was_in_air and not is_in_air:
for task in running_tasks:
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
await asyncio.get_event_loop().shutdown_asyncgens()
return

Expand Down

0 comments on commit 218169b

Please sign in to comment.