当前位置: 首页 > 后端技术 > Python

多处理池AttributeError-无法获取属性'func'

时间:2023-03-26 00:36:40 Python

python使用multiprocessing构建进程池报错:AttributeError:Can'tgetattribute'func'正确的进程池使用方式:importrequestsfromloggersimportloggerimportmultiprocessingdeffunc():try:foriinrange(1):response=requests.get('http://localhost:63000/')print(response.status_code,response.text)除了异常为错误:logger.exception(error)if__name__=="__main__":pool=multiprocessing.Pool(processes=56)foriinrange(100000):pool.apply_async(func)错误的进程池使用方式:importrequestsfromloggersimportloggerimportmultiprocessingpool=multiprocessing.Pool(processes=56)deffunc():try:foriinrange(1):response=requests.get('http://localhost:63000/')print(response.status_code,response.text)exceptExceptionaserror:logger.exception(error)if__name__=="__main__":对于我在范围内(100000):pool.apply_async(func)所以全局变量池必须放在if__name__=="__main__":