python 使用 multiprocessing 构建进程池 报错:AttributeError: Can't get attribute 'func'
正确的进程池使用方式:
import requests
from loggers import logger
import multiprocessing
def func():
try:
for i in range(1):
response = requests.get(
'http://localhost:63000/')
print(response.status_code, response.text)
except Exception as error:
logger.exception(error)
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=56)
for i in range(100000):
pool.apply_async(func)
错误的进程池使用方式:
import requests
from loggers import logger
import multiprocessing
pool = multiprocessing.Pool(processes=56)
def func():
try:
for i in range(1):
response = requests.get(
'http://localhost:63000/')
print(response.status_code, response.text)
except Exception as error:
logger.exception(error)
if __name__ == "__main__":
for i in range(100000):
pool.apply_async(func)
所以,必须要把 pool 这个全局变量放到 if __name__ == "__main__":
中去
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。