一个简单的python多协程实例
Posted 菜鸟的地盘
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的python多协程实例相关的知识,希望对你有一定的参考价值。
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多协程实例的主要内容,如果未能解决你的问题,请参考以下文章