我有一个队列变量和一个输入顺序。如果队列有什么内容,我必须强制输入命令停止。我不得不使用异步模块来完成这个任务。举个例子,让我们看看下面的代码:
import asyncio
import multiprocessing
import time
from aioconsole import ainput
def my_function(queue):
time.sleep(3)
queue.put(5)
async def my_loop(queue):
while True:
await asyncio.sleep(0.1)
if not queue.empty():
break
async def main():
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=my_function, args=(queue,))
p.start()
task1 = asyncio.create_task(ainput("Enter text:"))
task2 = asyncio.create_task(my_loop(queue))
await asyncio.wait([task1, task2], return_when='FIRST_COMPLETED')
try:
text = task1.result()
q = ""
except asyncio.exceptions.InvalidStateError:
text = ""
q = queue.get()
print('Doing stuff with input %s/%s...' % (text, q))
当队列上有什么内容或用户输入一些文本时,程序就会结束。我的真正的程序连续有几个这样的输入命令,如下所示:
import asyncio
import multiprocessing
import time
from aioconsole import ainput
def my_function(queue):
time.sleep(3)
queue.put(5)
async def my_loop(queue):
while True:
await asyncio.sleep(0.1)
if not queue.empty():
break
async def main():
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=my_function, args=(queue,))
p.start()
task1 = asyncio.create_task(ainput("Enter text:"))
task2 = asyncio.create_task(my_loop(queue))
await asyncio.wait([task1, task2], return_when='FIRST_COMPLETED')
try:
text = task1.result()
q = ""
except asyncio.exceptions.InvalidStateError:
text = ""
q = queue.get()
print('Doing stuff with input %s/%s...' % (text, q))
task1 = asyncio.create_task(ainput("Next: "))
task2 = asyncio.create_task(my_loop(queue))
await asyncio.wait([task1, task2], return_when='FIRST_COMPLETED')
try:
text = task1.result()
q = ""
except asyncio.exceptions.InvalidStateError:
text = ""
q = queue.get()
print('Doing stuff with input %s/%s...' % (text, q))
if __name__ == '__main__':
asyncio.run(main())
问题是,如果我第一次等待队列,第二次,我必须输入两次输入,就像第一次输入仍然在等待什么。如果用户什么都不写,你知道我怎么才能“打破”第一个输入?提前感谢
发布于 2022-05-24 01:11:38
如果我正确理解了您的意思,您可以在取消所有挂起的任务的步骤之间使用task.cancel()
:
import asyncio
import multiprocessing
import time
from aioconsole import ainput
def my_function(queue):
time.sleep(3)
queue.put(5)
async def my_loop(queue):
while True:
await asyncio.sleep(0.1)
if not queue.empty():
break
async def main():
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=my_function, args=(queue,))
p.start()
task1 = asyncio.create_task(ainput("Enter text:"))
task2 = asyncio.create_task(my_loop(queue))
tasks = [task1, task2]
await asyncio.wait(tasks, return_when="FIRST_COMPLETED")
try:
text = task1.result()
q = ""
except asyncio.exceptions.InvalidStateError:
text = ""
q = queue.get()
print()
print("1. Doing stuff with input %s/%s..." % (text, q))
# cancel all tasks:
for t in [task1, task2]:
t.cancel()
# Wait until all worker tasks are cancelled:
await asyncio.gather(*tasks, return_exceptions=True)
task1 = asyncio.create_task(ainput("Next: "))
task2 = asyncio.create_task(my_loop(queue))
tasks = [task1, task2]
await asyncio.wait(tasks, return_when="FIRST_COMPLETED")
try:
text = task1.result()
q = ""
except asyncio.exceptions.InvalidStateError:
text = ""
q = queue.get()
print()
print("2. Doing stuff with input %s/%s..." % (text, q))
if __name__ == "__main__":
asyncio.run(main())
打印(例如):
Enter text:
1. Doing stuff with input /5...
Next: Hello World
2. Doing stuff with input Hello World/...
https://stackoverflow.com/questions/72351945
复制相似问题