标签:fail rod json exce status sum time on() post
import xlrd,threading, requests,threading,json,time,math
import numpy as np
import gevent
from queue import Queue
products = Queue()
result = Queue()
def excel():
wb = xlrd.open_workbook(r‘E:/python/xt_user.xlsx‘)
sheet = wb.sheet_by_name(‘xt_user‘)
for a in range(sheet.nrows):
cells = sheet.row_values(a)
products.put(cells)
def consumer():
while not products.empty():
obj = products.get()
res = thread_test(obj)
print(res)
result.put(res)
def thread_test(object):
url = ‘‘
r = requests.post(url, data=json.dumps({}), headers={‘Content-Type‘:‘application/json‘})
#print(r.json())
return r.json()
excel()
g1 = gevent.spawn(consumer)
g2 = gevent.spawn(consumer)
g3 = gevent.spawn(consumer)
g1.join()
g2.join()
g3.join()
g4 = gevent.spawn(consumer)
g5 = gevent.spawn(consumer)
g6 = gevent.spawn(consumer)
g4.join()
g5.join()
g6.join()
time.sleep(10)
success = 0
fail = 0
while not result.empty():
obj = result.get()
if obj[‘status‘] == 200 :
print(obj)
success += 1
else:
print(‘请求失败‘)
fail += 1
print(‘成功个数:‘, success)
print(‘失败个数:‘, fail)
一个简单的python多协程实例
标签:fail rod json exce status sum time on() post
原文地址:https://www.cnblogs.com/huangguojin/p/13225193.html