Reputation: 39
There is a class about keyboard input and waits a few seconds after typing the key as follows:
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()))
When the key function is very many like caseA, Should I continue to write the words 'await' for all functions? Is there a way to omit or a better way?
Upvotes: 0
Views: 69
Reputation: 155495
There is no way to omit the await
. In this example you can obviously compress them using a for
loop, but you have to write await
before functions that return awaitables, such as async functions.
Upvotes: 1