推荐学习书目
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 · Jan 27, 2021 · 2934 views
    This topic created in 1958 days ago, the information mentioned may be changed or developed.
    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 replies    2021-01-28 17:56:54 +08:00
    linw1995
        1
    linw1995  
       Jan 27, 2021
    问问题用 gist 展示代码是美德。
    ByteRan
        2
    ByteRan  
       Jan 27, 2021
    问问题用 gist 展示代码是美德。
    linw1995
        3
    linw1995  
       Jan 27, 2021
    `async with await s.get(url) as response:` 这行写错了吧,光 async with 就好了
    dongcheng
        4
    dongcheng  
    OP
       Jan 27, 2021
    @linw1995 这么写没报错。不过我发现原因貌似在,semaphore = asyncio.Semaphore(10*multiprocessing.cpu_count())
    dongcheng
        5
    dongcheng  
    OP
       Jan 27, 2021
    semaphore 这个值好像不能随便填
    LeeReamond
        6
    LeeReamond  
       Jan 27, 2021
    问问题用 gist 展示代码是美德。
    qbqbqbqb
        7
    qbqbqbqb  
       Jan 28, 2021
    linw1995
        8
    linw1995  
       Jan 28, 2021
    @dongcheng 问题的代码里没见到有这个 semaphore 。再说你这个用法也不对
    deplives
        9
    deplives  
       Jan 28, 2021
    一个靠缩紧区分代码块的语言你给我一把梭的顶头写,你给谁看呢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   962 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 19:59 · PVG 03:59 · LAX 12:59 · JFK 15:59
    ♥ Do have faith in what you're doing.