python_asyncio_course/4_polling/polling.py

30 lines
906 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
async def print_message():
while True:
print("Имитация работы функции")
await asyncio.sleep(1)
async def interrupt_handler(interrupt_flag):
while True:
# Ждем установки флага.
await interrupt_flag.wait()
print("Произошло прерывание! В этом месте может быть установлен любой обработчик")
# Очищаем флаг для следующего использования
interrupt_flag.clear()
async def main():
interrupt_flag = asyncio.Event()
asyncio.create_task(print_message())
asyncio.create_task(interrupt_handler(interrupt_flag))
while True:
await asyncio.sleep(3)
# Устанавливаем флаг для прерывания
interrupt_flag.set()
asyncio.run(main())