File tree Expand file tree Collapse file tree 3 files changed +37
-13
lines changed Expand file tree Collapse file tree 3 files changed +37
-13
lines changed Original file line number Diff line number Diff line change @@ -16,8 +16,9 @@ async def run():
16
16
print (f"Drone discovered with UUID: { state .uuid } " )
17
17
break
18
18
19
- asyncio .ensure_future (print_camera_mode (drone ))
20
- asyncio .ensure_future (print_status (drone ))
19
+ print_mode_task = asyncio .ensure_future (print_mode (drone ))
20
+ print_status_task = asyncio .ensure_future (print_status (drone ))
21
+ running_tasks = [print_mode_task , print_status_task ]
21
22
22
23
print ("Setting mode to 'PHOTO'" )
23
24
try :
@@ -33,14 +34,20 @@ async def run():
33
34
except CameraError as error :
34
35
print (f"Couldn't take photo: { error ._result .result } " )
35
36
36
- # Shut down the running coroutines (here 'print_camera_mode ()' and
37
+ # Shut down the running coroutines (here 'print_mode ()' and
37
38
# 'print_status()')
39
+ for task in running_tasks :
40
+ task .cancel ()
41
+ try :
42
+ await task
43
+ except asyncio .CancelledError :
44
+ pass
38
45
await asyncio .get_event_loop ().shutdown_asyncgens ()
39
46
40
47
41
- async def print_camera_mode (drone ):
42
- async for camera_mode in drone .camera .mode ():
43
- print (f"Camera mode: { camera_mode } " )
48
+ async def print_mode (drone ):
49
+ async for mode in drone .camera .mode ():
50
+ print (f"Camera mode: { mode } " )
44
51
45
52
46
53
async def print_status (drone ):
Original file line number Diff line number Diff line change @@ -16,8 +16,10 @@ async def run():
16
16
print (f"Drone discovered with UUID: { state .uuid } " )
17
17
break
18
18
19
- asyncio .ensure_future (print_mission_progress (drone ))
20
- termination_task = asyncio .ensure_future (observe_is_in_air (drone ))
19
+ print_mission_progress_task = asyncio .ensure_future (print_mission_progress (drone ))
20
+
21
+ running_tasks = [print_mission_progress_task ]
22
+ termination_task = asyncio .ensure_future (observe_is_in_air (drone , running_tasks ))
21
23
22
24
mission_items = []
23
25
mission_items .append (MissionItem (47.398039859999997 ,
@@ -74,7 +76,7 @@ async def print_mission_progress(drone):
74
76
f"{ mission_progress .total } " )
75
77
76
78
77
- async def observe_is_in_air (drone ):
79
+ async def observe_is_in_air (drone , running_tasks ):
78
80
""" Monitors whether the drone is flying or not and
79
81
returns after landing """
80
82
@@ -85,7 +87,14 @@ async def observe_is_in_air(drone):
85
87
was_in_air = is_in_air
86
88
87
89
if was_in_air and not is_in_air :
90
+ for task in running_tasks :
91
+ task .cancel ()
92
+ try :
93
+ await task
94
+ except asyncio .CancelledError :
95
+ pass
88
96
await asyncio .get_event_loop ().shutdown_asyncgens ()
97
+
89
98
return
90
99
91
100
Original file line number Diff line number Diff line change @@ -35,9 +35,11 @@ async def run():
35
35
break
36
36
37
37
# Start parallel tasks
38
- asyncio .ensure_future (print_altitude (drone ))
39
- asyncio .ensure_future (print_flight_mode (drone ))
40
- termination_task = asyncio .ensure_future (observe_is_in_air (drone ))
38
+ print_altitude_task = asyncio .ensure_future (print_altitude (drone ))
39
+ print_flight_mode_task = asyncio .ensure_future (print_flight_mode (drone ))
40
+
41
+ running_tasks = [print_altitude_task , print_flight_mode_task ]
42
+ termination_task = asyncio .ensure_future (observe_is_in_air (drone , running_tasks ))
41
43
42
44
# Execute the maneuvers
43
45
print ("-- Arming" )
@@ -78,7 +80,7 @@ async def print_flight_mode(drone):
78
80
print (f"Flight mode: { flight_mode } " )
79
81
80
82
81
- async def observe_is_in_air (drone ):
83
+ async def observe_is_in_air (drone , running_tasks ):
82
84
""" Monitors whether the drone is flying or not and
83
85
returns after landing """
84
86
@@ -89,6 +91,12 @@ async def observe_is_in_air(drone):
89
91
was_in_air = is_in_air
90
92
91
93
if was_in_air and not is_in_air :
94
+ for task in running_tasks :
95
+ task .cancel ()
96
+ try :
97
+ await task
98
+ except asyncio .CancelledError :
99
+ pass
92
100
await asyncio .get_event_loop ().shutdown_asyncgens ()
93
101
return
94
102
You can’t perform that action at this time.
0 commit comments