关于python:multiprocessing-pool-AttributeError-Cant-get-attribute-func

67次阅读

共计 787 个字符,预计需要花费 2 分钟才能阅读完成。

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__": 中去

正文完
 0