私はasyncio
のPythonモジュールで遊んでいますが、私の単純なコードで何が問題なのか分かりません。非同期にタスクを実行することはありません。Asyncioはタスクを非同期に実行しません
#!/usr/bin/env python3
import asyncio
import string
async def print_num():
for x in range(0, 10):
print('Number: {}'.format(x))
await asyncio.sleep(1)
print('print_num is finished!')
async def print_alp():
my_list = string.ascii_uppercase
for x in my_list:
print('Letter: {}'.format(x))
await asyncio.sleep(1)
print('print_alp is finished!')
async def msg(my_msg):
print(my_msg)
await asyncio.sleep(1)
async def main():
await msg('Hello World!')
await print_alp()
await msg('Hello Again!')
await print_num()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()
はここでスクリプトが呼び出されたときの出力です:
Hello World!
Letter: A
Letter: B
Letter: C
Letter: D
Letter: E
Letter: F
Letter: G
Letter: H
Letter: I
Letter: J
Letter: K
Letter: L
Letter: M
Letter: N
Letter: O
Letter: P
Letter: Q
Letter: R
Letter: S
Letter: T
Letter: U
Letter: V
Letter: W
Letter: X
Letter: Y
Letter: Z
print_alp is finished!
Hello Again!
Number: 0
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9
print_num is finished!