为什么 asyncio.create_task 不运行该方法?

知乎

代码示例:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

asyncio create_task永远运行

Asyncio - create_task 阻塞線程

AttributeError:模块“ asyncio”没有属性“ create_task”

asyncio的call_soon失败,而create_task没有

asyncio.create_task()有什么作用?

等待只是Python Asyncio中create_task的语法糖吗?

如何在 asyncio create_task 中更新全局变量

为什么这个asyncio.Task永远不会完成取消?

为什么立即从asyncio Task中引发此异常?

为什么asyncio不总是使用执行程序?

为什么等待不等待asyncio.create_subprocess_exec()

为什么 asyncio.sleep 与 aiohttp websockets 一起使用时会冻结整个 Task(里面有 websocket)处于运行状态?

Python create_task在运行事件循环中不起作用

为什么“是”方法不运行?

asyncio.create_task与等待

asyncio模块如何工作,为什么我的更新样本同步运行?

在 Python asyncio 中,为什么按文件行处理块方法?

尝试使用asyncio时,为什么会出现“ RuntimeError:使用yield而不是Task Task中生成器的yield”的问题?

为什么不运行该程序,但是会生成?

为什么@Override方法不运行?

为什么引发asyncio.TimeoutError?

loop.create_task,asyncio.async / ensure_future和Task之间有什么区别?

为什么Task <T>不协变?

为什么Backbone的collection create()方法不填充ID?

为什么使用Asyncio不会减少Python的总体执行时间并同时运行函数?

asyncio方法不异步执行

为什么这样不运行?

为什么我的声纳扫描仪不运行会创建report-task.txt文件?

asyncio / aiohttp-create_task()阻止事件循环,在“此事件循环已在运行”中收集结果