为什么大多数 asyncio 示例使用 loop.run_until_complete()?
Why do most asyncio examples use loop.run_until_complete()?
我正在浏览 asyncio
的 Python 文档,我想知道为什么大多数示例使用 loop.run_until_complete()
而不是 Asyncio.ensure_future()
。
例如:https://docs.python.org/dev/library/asyncio-task.html
似乎 ensure_future
是展示非阻塞函数优点的更好方法。 run_until_complete
另一方面,像同步函数一样阻塞循环。
这让我觉得我应该使用 run_until_complete
而不是同时使用 ensure_future
和 loop.run_forever()
到 运行 多个协程。
run_until_complete
习惯于 运行 一个未来,直到它完成。它将阻止其后代码的执行。但是,它确实会导致事件循环 运行。任何已安排的未来将 运行 直到未来传递给 run_until_complete
完成。
给出这个例子:
import asyncio
async def do_io():
print('io start')
await asyncio.sleep(5)
print('io end')
async def do_other_things():
print('doing other things')
loop = asyncio.get_event_loop()
loop.run_until_complete(do_io())
loop.run_until_complete(do_other_things())
loop.close()
do_io
将 运行。完成后,do_other_things
将 运行。您的输出将是:
io start
io end
doing other things
如果您在 运行 宁 do_io
之前使用事件循环安排 do_other_things
,当前者等待时,控制将从 do_io
切换到 do_other_things
。
loop.create_task(do_other_things())
loop.run_until_complete(do_io())
这将为您提供以下输出:
doing other things
io start
io end
这是因为 do_other_things
排在 do_io
之前。有很多不同的方法可以获得相同的输出,但哪种方法有意义取决于您的应用程序实际执行的操作。所以我将把它作为 reader.
的练习
我想大多数人都没看懂create_task
。
当您 create_task
或 ensure_future
时,它已经安排好了。
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
task1 = asyncio.create_task(
say_after(1, 'hello')) # not block here
task2 = asyncio.create_task(
say_after(2, 'world'))
print(f"started at {time.strftime('%X')}") # time0
await task1 # block here!
print(f"finished at {time.strftime('%X')}")
await task2 # block here!
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
结果是
time0
print hello
time0+1
print world
time0+2
但如果您不要等待任务 1,请执行其他操作
async def main():
task1 = asyncio.create_task(
say_after(1, 'hello')) # not block here
print(f"finished at {time.strftime('%X')}") # time0
await asyncio.sleep(2) # not await task1
print(f"finished at {time.strftime('%X')}") # time0+2
asyncio.run(main())
它将执行任务 1 STILL
time0
print hello
time0+2
我正在浏览 asyncio
的 Python 文档,我想知道为什么大多数示例使用 loop.run_until_complete()
而不是 Asyncio.ensure_future()
。
例如:https://docs.python.org/dev/library/asyncio-task.html
似乎 ensure_future
是展示非阻塞函数优点的更好方法。 run_until_complete
另一方面,像同步函数一样阻塞循环。
这让我觉得我应该使用 run_until_complete
而不是同时使用 ensure_future
和 loop.run_forever()
到 运行 多个协程。
run_until_complete
习惯于 运行 一个未来,直到它完成。它将阻止其后代码的执行。但是,它确实会导致事件循环 运行。任何已安排的未来将 运行 直到未来传递给 run_until_complete
完成。
给出这个例子:
import asyncio
async def do_io():
print('io start')
await asyncio.sleep(5)
print('io end')
async def do_other_things():
print('doing other things')
loop = asyncio.get_event_loop()
loop.run_until_complete(do_io())
loop.run_until_complete(do_other_things())
loop.close()
do_io
将 运行。完成后,do_other_things
将 运行。您的输出将是:
io start
io end
doing other things
如果您在 运行 宁 do_io
之前使用事件循环安排 do_other_things
,当前者等待时,控制将从 do_io
切换到 do_other_things
。
loop.create_task(do_other_things())
loop.run_until_complete(do_io())
这将为您提供以下输出:
doing other things
io start
io end
这是因为 do_other_things
排在 do_io
之前。有很多不同的方法可以获得相同的输出,但哪种方法有意义取决于您的应用程序实际执行的操作。所以我将把它作为 reader.
我想大多数人都没看懂create_task
。
当您 create_task
或 ensure_future
时,它已经安排好了。
async def say_after(delay, what):
await asyncio.sleep(delay)
print(what)
async def main():
task1 = asyncio.create_task(
say_after(1, 'hello')) # not block here
task2 = asyncio.create_task(
say_after(2, 'world'))
print(f"started at {time.strftime('%X')}") # time0
await task1 # block here!
print(f"finished at {time.strftime('%X')}")
await task2 # block here!
print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
结果是
time0
print hello
time0+1
print world
time0+2
但如果您不要等待任务 1,请执行其他操作
async def main():
task1 = asyncio.create_task(
say_after(1, 'hello')) # not block here
print(f"finished at {time.strftime('%X')}") # time0
await asyncio.sleep(2) # not await task1
print(f"finished at {time.strftime('%X')}") # time0+2
asyncio.run(main())
它将执行任务 1 STILL
time0
print hello
time0+2