Как прервать цикл while на основе событийPython

Программы на Python
Ответить
Anonymous
 Как прервать цикл while на основе событий

Сообщение Anonymous »

В Python 2.7 я использую приведенный ниже код class1 для обнаружения некоторого события с помощью метода

Код: Выделить всё

check_for_the_event()

# Some class1
def check_for_the_event(self):
thread1 = threading.Timer(2, self.check_for_the_event)
thread1.start()
# some processing
if event is detected:
self.event_ok = True
else:
self.event_ok = False
затем я передаю экземпляр этого класса нижеуказанному классу, чтобы знать, когда событие включено или выключено, и соответствующим образом действую в зависимости от его значения, используя следующий код ниже:

Код: Выделить всё

              # execution of other part of the program (where self.another_class_instance.event_ok = False and self.some_other_condition is true)
current_time = current_time_some_cond = current_max_time = time.time()
while not self.another_class_instance.event_ok and self.some_other_condition:
while self.some_other_condition and not self.another_class_instance.event_ok:#self.some_other_condition takes up to 10 secs to be checked (minimum 5 secs)
if time.time() > current_time + 5:
current_time = time.time()
# some processing
else:
while not self.another_class_instance.event_ok:
#processing
if time.time() > current_max_time + 60 * 2 and not self.another_class_instance.event_ok:
#some other processing
if time.time() > current_time_some_cond + 10 and not self.cond1 and not self.another_class_instance.event_ok:
# some processing that takes 2-3 seconds
self.cond1 = True
current_time_some_cond = time.time()
elif self.cond1 and time.time() > current_time_some_cond + 10 and not self.another_class_instance.event_ok:
current_time_some_cond = time.time()
#some other processing that takes 2-3 seconds
else:
pass
else:
pass
Проблема в том, что выполнение программы в реальном времени (в классе 2) требует выполнения мгновенной проверки, и она не может дождаться завершения инструкций текущего цикла, и я хочу выйти из нее внешнего цикла while, если событие включено, без дальнейшей проверки каких-либо других условий.
на данный момент я выполняю несколько проверок для каждого оператора if или while, но есть ли асинхронный ввод-вывод метод, который выходит из цикла, когда событие возникает в нить?

Подробнее здесь: https://stackoverflow.com/questions/792 ... -on-events
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»