问题描述
有一类关于键盘输入的类,它在按下键后等待几秒钟,如下所示:
class Keyboard:
async def sendpacket(self,keyvalue,s):
print(f"keyboard write{keyvalue}")
await asyncio.sleep(s)
key = Keyboard()
async def caseA():
if random.randint(0,5) == 1:
await key.sendpacket('a',0.4)
await key.sendpacket('o',0.8)
else:
await key.sendpacket('b',1)
await key.sendpacket('b',0.5)
await key.sendpacket('b',0.5)
await key.sendpacket('5',0.1)
await key.sendpacket('y',0.1)
await key.sendpacket('e',0.1)
await key.sendpacket('i',0.1)
await key.sendpacket('a',0.2)
for i in range(5):
await key.sendpacket('f',0.5)
await key.sendpacket('1',0.5)
await key.sendpacket('2',0.5)
await key.sendpacket('e',0.5)
await key.sendpacket('i',1)
await key.sendpacket('a',2)
.
.
.
async def caseB():
while True:
x = random.randint(1,5)
print(x)
await asyncio.sleep(x)
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(caseA(),caseB()))
当很多关键功能(如caseA)时, 我是否应该继续为所有功能写“ await”一词? 有什么方法可以省略还是更好的方法?
解决方法
无法省略=COUNTIFS(X38:X43,"<= 1/2/2020",Y38:Y43,">= 28/2/2020")
。在此示例中,您显然可以使用await
循环对其进行压缩,但是您必须在返回可等待的函数(例如异步函数)之前编写for
。