ждать в отдельном потоке, не блокируя основной цикл событий, асинхронный питон - PullRequest
0 голосов
/ 03 октября 2019

У меня проблема на работе, когда мне приходится ждать 10 секунд, когда наступает событие InstrInstallSucceeded, без блокировки основного потока, я должен ждать появления InstrInstallFailed, то есть другими словами «ToolOn», «ToolOn», 'ToolOn 'должен появиться без ожидания.

import asyncio
from threading import Thread
import time
FLAG = True



async def sleep_loop(t, event):
    global FLAG
    print(event)
    if event == 'InstrInstallSucceeded':
        # spwan a seperate thread here such that 
        # toolon events are not blocked by the sleep
        await asyncio.sleep(t)
        FLAG = True
    if event == 'InstrInstallFailed':
        # and I want to update the FLAG whenever I see event == 'InstrInstallFailed'
        FLAG = False



async def keep_print():
    print(f'Beginning FLAG:: {FLAG}')
    while FLAG:
        pass
    print(f'End FLAG:: {FLAG}')



def start_loop(loop, t):
    print("in start loop")
    asyncio.set_event_loop(loop)
    for i in ['InstrInstallSucceeded', 'ToolOn','ToolOn', 'ToolOn', 'InstrInstallFailed']:
        loop.run_until_complete(asyncio.sleep(1))
        loop.run_until_complete(sleep_loop(t, i))

loop = asyncio.get_event_loop()
new_loop = asyncio.new_event_loop()
t = Thread(target=start_loop, args=(new_loop,10))
t.start()
coro = keep_print()
loop.run_until_complete(coro)

вывод

in start loop
Beginning FLAG:: True
Executing <Task pending coro=<sleep() running at /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py:482> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x1043f2be8>()] created at /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py:284> cb=[_run_until_complete_cb() at /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py:185] created at /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py:452> took 0.118 seconds
InstrInstallSucceeded
ToolOn
ToolOn
ToolOn
InstrInstallFailed
End FLAG:: False
Executing <Task finished coro=<keep_print() done, defined at fut.py:21> result=None created at /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py:452> took 15.756 seconds

РЕДАКТИРОВАТЬ: используя python 3.6.7

import asyncio

async def dispatch_event(event, alert):
    print(event)
    if event == 'InstrInstallSucceeded':
        # spawn a coroutine if you need something done in parallel
        #asyncio.create_task(xxx())
        await asyncio.sleep(10)
    if event == 'InstrInstallFailed':
        await asyncio.sleep(.5)

    # alert the watcher(s) of the event that was dispatched
    alert.last_event = event
    alert.set()

async def keep_print(alert):
    while True:
        print(f'Beginning FLAG:: {alert.last_event}')
        await alert.wait()
        alert.clear()
        print(f'End FLAG:: {alert.last_event}')

async def main():
    alert = asyncio.Event()
    alert.last_event = None
    # spawn keep_print in the "background"
    loop = asyncio.get_event_loop()
    t = loop.create_task(keep_print(alert))
    for i in ['InstrInstallSucceeded', 'ToolOn','ToolOn', 'ToolOn', 'InstrInstallFailed']:
        await asyncio.sleep(1)
        await dispatch_event(i, alert)
    await asyncio.sleep(1)
    t.cancel()
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

редактировать в соответствии с предложением @ user418 .....

async def dispatch_event(event,alert):

    alert.last_event = event
    alert.set()

    print(event)
    if event == 'InstrInstallSucceeded':
        # spawn a coroutine if you need something done in parallel
        #asyncio.create_task(xxx())
        await asyncio.sleep(10)
    if event == 'InstrInstallFailed':
        await asyncio.sleep(.5)
    # alert the watcher(s) of the event that was dispatched

1 Ответ

1 голос
/ 03 октября 2019

Потоки и asyncio не сочетаются друг с другом, кроме как при определенных обстоятельствах (например, реализация run_in_executor). Вместо того, чтобы создавать новые темы, создайте новые сопрограммы.

Например:

import asyncio

async def dispatch_event(event, alert):
    print(event)
    if event == 'InstrInstallSucceeded':
        # spawn a coroutine if you need something done in parallel
        #asyncio.create_task(xxx())
        await asyncio.sleep(1)
    if event == 'InstrInstallFailed':
        await asyncio.sleep(.5)

    # alert the watcher(s) of the event that was dispatched
    alert.last_event = event
    alert.set()

async def keep_print(alert):
    while True:
        print(f'Beginning FLAG:: {alert.last_event}')
        await alert.wait()
        alert.clear()
        print(f'End FLAG:: {alert.last_event}')

async def main():
    alert = asyncio.Event()
    alert.last_event = None
    # spawn keep_print in the "background"
    t = asyncio.create_task(keep_print(alert))
    for i in ['InstrInstallSucceeded', 'ToolOn','ToolOn', 'ToolOn', 'InstrInstallFailed']:
        await asyncio.sleep(1)
        await dispatch_event(i, alert)
    await asyncio.sleep(1)
    t.cancel()

asyncio.run(main())
...