zoukankan      html  css  js  c++  java
  • 一个简单的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)




  • 相关阅读:
    列表
    Pyunit测试框架
    Jmeter性能测试入门(转)
    Monkey for iOS(CrashMonkey4IOS)
    美化iTerm2
    不经过 App store 的安装方式(转)
    Mac上部署JDK/Ant/Jmeter/Jenkins
    display:inline-block元素之间错位问题
    vue项目安装依赖报错没有python
    正则匹配所有li标签内容
  • 原文地址:https://www.cnblogs.com/huangguojin/p/13225193.html
Copyright © 2011-2022 走看看