wait any structure more readable

This commit is contained in:
Oskar Weigl
2018-04-21 15:41:33 -07:00
parent 98106a3dc9
commit 52be9da107
2 changed files with 10 additions and 9 deletions
+2 -2
View File
@@ -13,6 +13,7 @@ import array
import fractions
import usb.core
import odrive.discovery
from odrive.utils import Event
from odrive.dfuse import *
try:
@@ -244,8 +245,7 @@ def launch_dfu(args, app_shutdown_token):
serial_number = args.serial_number
find_odrive_cancellation_token = threading.Event()
app_shutdown_token.subscribe(lambda: find_odrive_cancellation_token.set())
find_odrive_cancellation_token = Event(app_shutdown_token)
print("Waiting for ODrive...")
+8 -7
View File
@@ -9,6 +9,7 @@ import threading
import platform
import subprocess
import os
from odrive.utils import Event
try:
if platform.system() == 'Windows':
@@ -33,7 +34,7 @@ def start_liveplotter(get_var_callback):
import matplotlib.pyplot as plt
cancellation_token = threading.Event()
cancellation_token = Event()
global vals
vals = []
@@ -175,7 +176,7 @@ class Event():
handler()
finally:
self._mutex.release()
return lambda: self.unsubscribe(handler)
return handler
def unsubscribe(self, handler):
self._mutex.acquire()
@@ -201,16 +202,16 @@ class Event():
def wait_any(*events, timeout=None):
"""
Blocks until any of the specified events are triggered.
Returns the number of the event that was triggerd or raises
Returns the index of the event that was triggerd or raises
a TimeoutException
"""
or_event = threading.Event()
unsubscribe_functions = []
subscriptions = []
for event in events:
unsubscribe_functions.append(event.subscribe(lambda: or_event.set()))
subscriptions.append((event, event.subscribe(lambda: or_event.set())))
or_event.wait(timeout=timeout)
for unsubscribe_function in unsubscribe_functions:
unsubscribe_function()
for event, sub in subscriptions:
event.unsubscribe(sub)
for i in range(len(events)):
if events[i].is_set():
return i