多进程里嵌套协程,并发数增加的时候,程序卡死是怎么回事? - V2EX
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
dongcheng
V2EX    Python

多进程里嵌套协程,并发数增加的时候,程序卡死是怎么回事?

  •  
  •   dongcheng 2021-01-27 17:09:56 +08:00 2489 次点击
    这是一个创建于 1722 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import multiprocessing
    from multiprocessing import Semaphore
    import time
    import asyncio
    import aiohttp
    import requests
    import random,time, datetime
    import utils
    import json

    def start_loop(turn):

    request_url = 'https://www.baidu.com'

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    tasks = []
    for num in range(2000):

    func = aio_request(request_url,turn,loop)
    task = asyncio.ensure_future(func)
    tasks.append(task)

    try:
    ret = loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()
    except Exception as e:
    print('loop 中断或者完成',e)

    return turn

    async def aio_request(url,headers,turn,loop):

    async with aiohttp.ClientSession() as s:
    async with await s.get(url) as response:
    print('请求内容', url)
    # response.read()二进制(.content )
    result = await response.json()
    print(url, '获取内容完成')

    def main():

    start = time.time()

    pool = multiprocessing.Pool(processes = 30)
    for turn in range(10):
    pool.apply_async(start_loop, (turn, ))

    pool.close()
    pool.join()
    print('总耗时:',time.time()-start)
    if __name__ == "__main__":
    main()

    这是个简单例子。主要逻辑是,多进程跑协程任务,协程处理并发 2000 个地址以上。奇怪的地方在于,我这个例子也能跑,但放到项目里就卡死。而且并发 20 能跑,2000 就卡死。把这个协程单独运行(非进程内)也可以跑,不知道是不是阻塞了。之前实在没并发编程经验,不知道算不算很低级的错误。。。
    9 条回复    2021-01-28 17:56:54 +08:00
    linw1995
        1
    linw1995  
       2021-01-27 18:49:52 +08:00
    问问题用 gist 展示代码是美德。
    tanranran
        2
    tanranran  
       2021-01-27 18:1:22 +08:00
    问问题用 gist 展示代码是美德。
    linw1995
        3
    linw1995  
       2021-01-27 18:52:40 +08:00
    `async with await s.get(url) as response:` 这行写错了吧,光 async with 就好了
    dongcheng
        4
    dongcheng  
    OP
       2021-01-27 19:01:28 +08:00
    @linw1995 这么写没报错。不过我发现原因貌似在,semaphore = asyncio.Semaphore(10*multiprocessing.cpu_count())
    dongcheng
        5
    dongcheng  
    OP
       2021-01-27 19:02:00 +08:00
    semaphore 这个值好像不能随便填
    LeeReamond
        6
    LeeReamond  
       2021-01-27 19:36:35 +08:00
    问问题用 gist 展示代码是美德。
    qbqbqbqb
        7
    qbqbqbqb  
       2021-01-28 00:14:05 +08:00
    linw1995
        8
    linw1995  
       2021-01-28 12:51:41 +08:00
    @dongcheng 问题的代码里没见到有这个 semaphore 。再说你这个用法也不对
    deplives
        9
    deplives  
       2021-01-28 17:56:54 +08:00
    一个靠缩紧区分代码块的语言你给我一把梭的顶头写,你给谁看呢
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2710 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 15:14 PVG 23:14 LAX 08:14 JFK 11:14
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86