gpt4 book ai didi

python-3.x - 为什么 asyncio.create_task 不运行该方法?

转载 作者:行者123 更新时间:2023-12-04 15:41:43 59 4
gpt4 key购买 nike

代码示例:

async def download_page(session, url):
print(True)


async def downloader_init(session):
while True:
url = await download_queue.get()
task = asyncio.create_task(download_page(session, url))
print(task)
print(f"url: {url}")


async def get_urls(url):
while True:
try:
url = find_somewhere_url
await download_queue.put(url)
except NoSuchElementException:
break
return True


async def main():
async with aiohttp.ClientSession(headers=headers) as session:
get_urls_task = asyncio.create_task(get_urls(url))
downloader_init_task = asyncio.create_task(downloader_init(session))

asyncio.gather(get_urls_task, downloader_init_task)


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())

输出:
<Task pending coro=<download_page() running at main.py:69>>
url: https://someurl.com/example
<Task pending coro=<download_page() running at main.py:69>>
url: https://someurl.com/example
<Task pending coro=<download_page() running at main.py:69>>
url: https://someurl.com/example

为什么是方法 download_page没有执行?
奇怪的是,脚本刚刚结束它的工作,任何地方都没有错误。 downloader_init应该无休止地工作,但事实并非如此。

download_queue , 方法 get_urls在找到链接时添加链接,之后它停止工作。 downloader_init队列中出现新链接时应立即执行,但仅在 get_urls 时才开始工作。已经完成了它的工作。

最佳答案

试试这个:

注意:您的问题不在于任务创建,而是因为asyncio.gather 没有等待部分。

import asyncio
import aiohttp


async def download_page(session, url):
# Dummy function.
print(f"session={session}, url={url}")


async def downloader_init(session):
while True:
url = await download_queue.get()
task = asyncio.create_task(download_page(session, url))
print(f"task={task}, url={url}")


async def get_urls(url):
while True:
try:
url = find_somewhere_url()
await download_queue.put(url)
except NoSuchElementException:
break


async def main():
async with aiohttp.ClientSession(headers=headers) as session:
get_urls_task = asyncio.create_task(get_urls(url))
downloader_init_task = asyncio.create_task(downloader_init(session))

# Use await here to make it finish the tasks.
await asyncio.gather(get_urls_task, downloader_init_task)


if __name__ == "__main__":
# Use this as it deals with the loop creation, shutdown,
# and other stuff for you.
asyncio.run(main()) # This is new in Python 3.7

关于python-3.x - 为什么 asyncio.create_task 不运行该方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57630209/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com