python实现多线程

参考

https://www.cnblogs.com/luyuze95/p/11289143.html

import threading
import urllib3
import jsonpath
from requests import request
from ABtest.guid import GetGuid

# 解决认证报错
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class ABTest:

    def ab_test_result(self, ab_name):
        url = "http:xxx"
        headers = {"Content-Type": "application/json"}
        ori_data = {
            "channel": "cl-abtest-api",
            "apiVersion": "1.0",
            "postData": {"testNames": [
                "zjctesti",
                "zjctestu"
            ],
                "deviceId": ""}
        }
        ori_data["postData"]["deviceId"] = GetGuid().get_guid()
        # print(ori_data)
        response = request(method="post", url=url, headers=headers, json=ori_data, verify=False)
        res = response.json()
        group_name = jsonpath.jsonpath(res, "$..[?(@.name=='{}')].result".format(ab_name))[0]
        # print(group_name)
        group_name2 = group_name["groupName"]
        # print(group_name2)
        res_headers = response.headers
        RequestId = res_headers["RequestId"]
        # print(RequestId)
        return group_name2

    def check_ab_result(self, num, ab_name, index_0_name, index_1_name, index_2_name, index_3_name):
        group_name_list = []
        for i in range(num):
            group_name = self.ab_test_result(ab_name)
            group_name_list.append(group_name)
        group_name_list_length = len(group_name_list)
        print("group_name_list_length:{}".format(group_name_list_length))
        index_0_length = group_name_list.count(index_0_name)
        index_1_length = group_name_list.count(index_1_name)
        index_2_length = group_name_list.count(index_2_name)
        index_3_length = group_name_list.count(index_3_name)
        print(index_0_length, index_1_length, index_2_length, index_3_length)
        percent_0 = "{:.6f}".format(index_0_length / group_name_list_length)
        percent_1 = "{:.6f}".format(index_1_length / group_name_list_length)
        percent_2 = "{:.6f}".format(index_2_length / group_name_list_length)
        percent_3 = "{:.6f}".format(index_3_length / group_name_list_length)
        result = "{}:{},{}:{},{}:{},{}:{}" \
            .format(index_0_name, percent_0, index_1_name, percent_1, index_2_name, percent_2, index_3_name, percent_3)
        print(result)
        return result


if __name__ == "__main__":
    abtest = ABTest()
    # result = abtest.check_ab_result(5, "zjctestu", "11", "22", "", None)
    t1 = threading.Thread(target=abtest.check_ab_result, args=(100, "zjctestu", "11", "22", "", None))
    t2 = threading.Thread(target=abtest.check_ab_result, args=(100, "zjctestu", "11", "22", "", None))
    t1.start()
    t2.start()
 t1 = threading.Thread(target=方法名(不加括号), args=(方法传入的参数))
 t1.start()

 

posted @ 2020-07-10 17:13  mkay  阅读(190)  评论(0编辑  收藏  举报