V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ohayoo
V2EX  ›  Python

Python asyncio 启动问题求助

  •  
  •   ohayoo · 2023-02-09 11:16:32 +08:00 · 1800 次点击
    这是一个创建于 444 天前的主题,其中的信息可能已经有所发展或是发生改变。

    求助大佬们,本菜鸡非专业程序员,只会点皮毛,偶尔做个小工具都是网上抄代码,东拼西凑,现在遇到一个这样的问题不知道怎么解决,还望大佬们能帮忙看看。

    我的异步函数第一种方式启动,可以正常运行,但我现在想要换成第二种启动方式,就会报错 This event loop is already running ,可能有大佬想问,第一种方式可以正常跑就行了,为啥非要作死换成第二种呢?是因为 fastapi 的路由函数是异步的,我需要在路由函数里面调用前面这个 ip 查询的异步函数,所以模拟了这样一段代码前来求助。

    想问下要怎么改才能在 aaa 这个异步函数里面启动前面的 main 函数呢?

    import aiohttp
    import asyncio
    
    iplist = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
    result = {}
    
    async def get_ipinfo(ip, domain, sem):
        params = {'ip': ip, 'dns': domain}
        async with sem:
            async with aiohttp.ClientSession() as session:
                async with session.get('http://www.abc.com/', params=params) as resp:
                    res = await resp.text()
                    #res 结果写入全局变量字典中,代码省略
    
    
    async def main(domain, sem):
        tasks = [get_ipinfo(ip, domain, sem) for ip in iplist]
        await asyncio.gather(*tasks)
    
    
    ################   启动方式 1  ##############
    #上面的异步函数启动方式 1 ,可以正常运行
    
    if __name__ == "__main__":
        sem = asyncio.Semaphore(20)
        loop = asyncio.get_event_loop()
        try:
            loop.run_until_complete(main('www.test.com', sem))
        finally:
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()
        print(result)
    
    
    ####################   启动方式 2  #####################
    #上面的异步函数启动方式 2 ,报错
    #     raise RuntimeError('This event loop is already running')
    # RuntimeError: This event loop is already running
    #
    # During handling of the above exception, another exception occurred:
    
    async def run(domain):
        sem = asyncio.Semaphore(20)
        loop = asyncio.get_event_loop()
        try:
            loop.run_until_complete(main(domain, sem))
        finally:
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()
    
    
    async def aaa(domain):
        await run(domain)
        print(result)
    
    
    asyncio.run(aaa('www.test.com'))
    
    
    6 条回复    2023-02-09 12:22:54 +08:00
    jenlors
        1
    jenlors  
       2023-02-09 11:27:26 +08:00
    第二种方式,run 里面就不用再用 run_until_complete 的方式了,直接 await
    karlkor
        2
    karlkor  
       2023-02-09 11:29:58 +08:00
    在 async 函数里面执行 coroutine 可以使用 asyncio.create_task, 也就是说在你的第二个 case 里面用 asyncio.create_task 替换掉 loop.run_until_complete
    mylifcc
        3
    mylifcc  
       2023-02-09 11:38:07 +08:00
    我两个都能运行
    mylifcc
        4
    mylifcc  
       2023-02-09 11:39:33 +08:00
    原来是没保存 这网站怎么删评论啊
    mylifcc
        5
    mylifcc  
       2023-02-09 11:42:05 +08:00
    async def run(domain):
    sem = asyncio.Semaphore(20)
    await main(domain, sem)
    这样就可以了
    ohayoo
        6
    ohayoo  
    OP
       2023-02-09 12:22:54 +08:00
    @jenlors @karlkor @mylifcc 感谢大佬们,两种方式都可以,
    官方文档有写,自己眼瞎居然没看到
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3910 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:13 · PVG 13:13 · LAX 22:13 · JFK 01:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.