• python3-使用进程方式进行并发请求


    1:使用HTTP长连接的方式进行发送请求:

    代码如下:

    import os
    import requests
    from requests.adapters import HTTPAdapter
    from requests.exceptions import ConnectionError

    class AliveRequests(object):

        # 会话池
        session_pool = {}

        def __enter__(self):
            self.session()
            return self

        def __exit__(self, *excinfo):
            pass

        @staticmethod
        def session():
            cur_pid = os.getpid()
            if cur_pid not in list(AliveRequests.session_pool.keys()):
                session = requests.session()
                session.mount('http://', HTTPAdapter(pool_maxsize=1000, pool_block=True))
                AliveRequests.session_pool[cur_pid] = session
            return AliveRequests.session_pool[cur_pid]

        @staticmethod
        def request(method=None, url=None, headers=None, files=None, data=None, params=None, auth=None, cookies=None, hooks=None, json=None, timeout=None):
            session = AliveRequests.session()
            request = requests.Request(method, url, headers, files, data, params, auth, cookies, hooks, json)
            while True:
                try:
                    with session.send(session.prepare_request(request), timeout=(10, timeout)) as response:
                        return response
                except ConnectionError as err:
                    err_obj = err.message if isinstance(err.message, Exception) else err
                    if "BadStatusLine" in err_obj.message or "reset by peer" in err_obj.message:
                        continue
                    else:
                        raise err
                except Exception as ex:
                    raise ex

    并发请求:

    from multiprocessing import Pool

    def work(self):
            datas = []
            po = Pool(n)  # 设置进程数
            res_list = po.map(func(函数名), 参数列表)   # 传入参数列表,传递给函数时参数列表遍历之后将每个元素依此传递,
            for res in iter(res_list):
                accessToken = res[0].json()["data"]["access_token"] if res[0].status_code == 200 and "data" in res[0].json() else ""
                dict_s = {"name": res[2][0],
                          "accessToken":accessToken,
                          "user_id":res[2][1],
                          "time": res[1]}
                datas.append(dict_s)
            return datas

    统计发起请求的事件,单位(s)

    resp = AliveRequests.request('POST', self.login_url, params=params, headers=self.headers)

    resp.elapsed.total_seconds()

  • 相关阅读:
    高级软件工程2017第2次作业—— 个人项目:四则运算题目生成程序(基于控制台)
    高级软件工程2017第1次作业
    2017高级软件工程 课程信息
    15061009-第0次作业(阅读与感想)
    新的開始!第一次作業
    软工实践第一次作业
    OO第四单元作业总结
    OO第二单元作业总结
    OO第一单元作业总结
    Dell BOSS 卡是什么
  • 原文地址:https://www.cnblogs.com/zhouzetian/p/12017915.html
Copyright © 2020-2023  润新知