在单独的线程中等待而不会阻塞主事件循环,异步python

时间:2019-10-03 02:46:44

标签: python-3.x asynchronous python-asyncio

我在工作时遇到一个问题,当InstrInstallSucceeded事件出现时,我必须等待10秒,而又不会阻塞主线程,我应该等待InstrInstallFailed出现,换句话说就是'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 个答案:

答案 0 :(得分:1)

除非在特定情况下(例如run_in_executor的实现),否则线程和asyncio不会并在一起。代替生成新线程,而是生成新协程。

例如:

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())