Python 3.x 在不阻塞主eventloop的单独线程中等待,异步python
我在工作中遇到了一个问题,当InstrinstallSuccessed事件出现时,我必须等待10秒,在不阻塞主线程的情况下,我应该等待InstrInstallFailed出现,因此换句话说,“ToolOn”、“ToolOn”、“ToolOn”应该在不等待的情况下出现Python 3.x 在不阻塞主eventloop的单独线程中等待,异步python,python-3.x,asynchronous,python-asyncio,Python 3.x,Asynchronous,Python Asyncio,我在工作中遇到了一个问题,当InstrinstallSuccessed事件出现时,我必须等待10秒,在不阻塞主线程的情况下,我应该等待InstrInstallFailed出现,因此换句话说,“ToolOn”、“ToolOn”、“ToolOn”应该在不等待的情况下出现 import asyncio from threading import Thread import time FLAG = True async def sleep_loop(t, event): global FL
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
按照@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
线程和异步IO不会同时进行,除非在特定情况下,例如的实现。生成新的协同程序,而不是生成新的线程 例如:
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())
好的,这对我来说不起作用,因为我将dispatch_事件中的wait asyncio.sleep10的时间增加到10秒,条件是event='instrinstallsuccessed':它似乎在那里等待10秒,然后继续。另外,我使用的是python 3.6.7,所以我也必须做一些修改,好吧,如果我创建一个新的事件循环,我可以在单独的循环中进行等待,但是我的新代码有一个新问题,在这里TLDR;我想做的是,当新的_事件中的时间用完时,将FLAG设置为False_loop@VaibhavChauhan您可以将警报设置移到dispatch_事件的开头。Asyncio是单线程的,您根本不需要单独的线程和事件循环。据我所知,我按照@user481…..的要求尝试了您的建议,在我的编辑中使用了它,但这对我没有多大帮助。请坐look@VaibhavChauhan您是否尝试过使用asyncio.create_任务(如评论中所示)?我想争取否决票,我想知道为什么我的问题被否决?
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())