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

2021-01-27 17:09:56 +08:00
 dongcheng
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 就卡死。把这个协程单独运行(非进程内)也可以跑,不知道是不是阻塞了。之前实在没并发编程经验,不知道算不算很低级的错误。。。
1969 次点击
所在节点    Python
9 条回复
linw1995
2021-01-27 18:49:52 +08:00
问问题用 gist 展示代码是美德。
tanranran
2021-01-27 18:51:22 +08:00
问问题用 gist 展示代码是美德。
linw1995
2021-01-27 18:52:40 +08:00
`async with await s.get(url) as response:` 这行写错了吧,光 async with 就好了
dongcheng
2021-01-27 19:01:28 +08:00
@linw1995 这么写没报错。不过我发现原因貌似在,semaphore = asyncio.Semaphore(10*multiprocessing.cpu_count())
dongcheng
2021-01-27 19:02:00 +08:00
semaphore 这个值好像不能随便填
LeeReamond
2021-01-27 19:36:35 +08:00
问问题用 gist 展示代码是美德。
qbqbqbqb
2021-01-28 00:14:05 +08:00
linw1995
2021-01-28 12:51:41 +08:00
@dongcheng 问题的代码里没见到有这个 semaphore 。再说你这个用法也不对
deplives
2021-01-28 17:56:54 +08:00
一个靠缩紧区分代码块的语言你给我一把梭的顶头写,你给谁看呢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/748977

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX