异步多线程处理

import asyncio,time,requests

import threading, time, requests
url = "http://www.jd.com"
total = 0
suc = 0
fail = 0
exception = 0
maxtime=0
mintime=100
gt3=0
lt3=0
class RequestThread(threading.Thread):
    def __init__(self, thread_name):  
        threading.Thread.__init__(self)  
        self.test_count = 0
    def run(self):
        self.test_performace()
    def test_performace(self):  
            global total
            global suc
            global fail
            global exception
            global gt3
            global lt3
            try:  
                st = time.time()  
                conn = requests.get(url)
                res = conn.status_code
                if res== 200:
                    total+=1
                    suc+=1
                else:
                    total+=1
                    fail+=1
                time_span = time.time()-st  
                print ('%s:%f\n'%(self.name,time_span)  )
                self.maxtime(time_span)  
                self.mintime(time_span)  
                if time_span>3:
                    gt3+=1
                else:
                    lt3+=1
            except Exception as e:  
                print (e  )
                total+=1
                exception+=1
    def maxtime(self,ts):  
            global maxtime
            print (ts)
            if ts>maxtime:
                maxtime=ts
    def mintime(self,ts):  
            global mintime
            if ts<mintime:
                mintime=ts
@asyncio.coroutine
async def hello():
    thread_count = 1000
    i = 0  
    while i <= thread_count:  
        t = RequestThread("线程:" + str(i))
        t.start()  
        i += 1
start_time=time.time()
loop=asyncio.get_event_loop()
loop.run_until_complete(hello())
loop.close()
print('异步处理时间:%s'%(time.time()-start_time))

  

转载于:https://www.cnblogs.com/leiziv5/p/7244429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值