threading_test.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*-coding:utf-8-*-
  2. import requests
  3. import random
  4. import json
  5. import time
  6. import threading
  7. class MyThread(threading.Thread):
  8. def __init__(self, func, args, name=''):
  9. threading.Thread.__init__(self)
  10. self.name = name
  11. self.func = func
  12. self.args = args
  13. def run(self):
  14. self.result = self.func(self.args)
  15. def get_result(self):
  16. try:
  17. return self.result
  18. except Exception:
  19. return None
  20. url = 'http://106.14.187.241:5555/elab-marketing-user//zhidi/activity/concurrent/turntableActivity'
  21. headers_2 = {'content-type': "application/json", 'Authorization': 'APP appid = 4abf1a,token = 9480295ab2e2eddb8',
  22. 'dsNo': 'source2'}
  23. def loop(nloop):
  24. response = requests.post(url, json=nloop, headers=headers_2)
  25. return response.text
  26. # 15
  27. def main():
  28. result_list = []
  29. threads = []
  30. people = 200
  31. times = range(people)
  32. for i in times:
  33. params = {
  34. "actionType": 1,
  35. "activityId": 8939,
  36. "brandId": 1,
  37. "customerId": 223222 + random.randint(5000, 9000),
  38. "houseId": 10110,
  39. "mobile": "15622363" + str(random.randint(1000, 9999)),
  40. "shareActivityId": 0,
  41. "uuid": ""
  42. }
  43. t = MyThread(loop, params, ' 第' + str(i + range(i + 1)[0]) + '线程')
  44. threads.append(t)
  45. for i in range(people): # start threads 此处并不会执行线程,而是将任务分发到每个线程,同步线程。等同步完成后再开始执行start方法
  46. threads[i].start()
  47. for i in range(people): # jion()方法等待线程完成
  48. threads[i].join()
  49. result = threads[i].get_result()
  50. result_list.append(json.loads(result))
  51. return result_list
  52. def analysis_result(result_list):
  53. # 抽奖结果统计
  54. dict_data = {}
  55. # 中奖
  56. prize_count = 0
  57. # 谢谢参与
  58. think_prize = 0
  59. # 抽奖失败
  60. failed_prize = 0
  61. for x in result_list:
  62. success = x['success']
  63. if success is True:
  64. # 抽奖成功,统计具体的抽奖情况
  65. single = x['single']
  66. name = single['name']
  67. if name in dict_data.keys():
  68. count = dict_data[name]
  69. dict_data[name] = count + 1
  70. else:
  71. dict_data[name] = 1
  72. thinks = single['thinks']
  73. if str(thinks) == str(1):
  74. think_prize += 1
  75. else:
  76. prize_count += 1
  77. else:
  78. # 抽奖失败统计
  79. failed_prize += 1
  80. all = prize_count + think_prize
  81. for key in dict_data.keys():
  82. count = dict_data[key]
  83. if all > 0:
  84. print(key + ': ' + str(count), ",占比: " + str(count/all))
  85. else:
  86. print(key + ': ' + str(count))
  87. print('------------,参与抽奖人数:' + str(all))
  88. print("中奖:" + str(prize_count), ",中奖率:" + str(prize_count / all))
  89. print("谢谢参与; " + str(think_prize), ",谢谢参与率: " + str(think_prize / all))
  90. # print("抽奖失败:" + str(failed_prize), ",抽奖失败率; " + str(failed_prize / len(result_list)))
  91. if __name__ == '__main__':
  92. result = main()
  93. analysis_result(result)