今天研究了下python3的新特性 asynico,试了试aiohttp协程效果,单核QPS在500~600之间,性能还可以。
1import aiohttp 2import asyncio 3import hashlib 4import time 5from asyncio import Queue 6 7 8class Fetch: 9 def __init__(self): 10 self.work_queue = Queue() 11 self.max_loop = 10000 12 self.host = "http://14.29.5.29/XXXX" 13 self.payload = {"planId": 10000007, "activityId": 1002, "label": 1, 14 "key": "98214ecfe6b9ae8855e3ac6509ad940f", "keyType": "imei", 15 "batchId": 1, "token": "395La7f9x9x"} 16 17 async def get_url(self, host, payload): 18 async with aiohttp.ClientSession() as session: 19 async with session.post(host, data=payload) as resp: 20 text = await resp.text() 21 if "1" in text: 22 print(text, payload["key"]) 23 24 async def consumer(self): 25 while True: 26 param = await self.work_queue.get() 27 if param: 28 await self.get_url(self.host, param) 29 self.work_queue.task_done() 30 else: 31 break 32 33 async def producer(self): 34 i = 0 35 string = '866260035710238' 36 while 1: 37 if i: 38 md5_str = hashlib.md5(string.encode('utf-8')) 39 self.payload["key"] = md5_str.hexdigest() 40 string = str(int(string) + 1) 41 await self.work_queue.put(self.payload.copy()) #必须要 42 i += 1 43 if i > self.max_loop: 44 break 45 46 async def run(self): 47 await self.producer() 48 print('start consumer...') 49 50 tasks = [ 51 loop.create_task(self.consumer()) 52 for i in range(10) 53 ] 54 55 await self.work_queue.join() 56 print('end join') 57 for task in tasks: 58 task.cancel() 59 60t1 = time.time() 61loop = asyncio.get_event_loop() 62test = Fetch() 63loop.run_until_complete(test.run()) 64loop.close() 65print(time.time() - t1)