性能测试: Python3 利用asynico协程系统构建生产消费模型

今天研究了下python3的新特性 asynico ,试了试 aiohttp 协程效果,单核QPS在500~600之间,性能还可以。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import aiohttp
import asyncio
import hashlib
import time
from asyncio import Queue


class Fetch:
def __init__(self):
self.work_queue = Queue()
self.max_loop = 10000
self.host = "http://14.29.5.29/XXXX"
self.payload = {"planId": 10000007, "activityId": 1002, "label": 1,
"key": "98214ecfe6b9ae8855e3ac6509ad940f", "keyType": "imei",
"batchId": 1, "token": "395La7f9x9x"}

async def get_url(self, host, payload):
async with aiohttp.ClientSession() as session:
async with session.post(host, data=payload) as resp:
text = await resp.text()
if "1" in text:
print(text, payload["key"])

async def consumer(self):
while True:
param = await self.work_queue.get()
if param:
await self.get_url(self.host, param)
self.work_queue.task_done()
else:
break

async def producer(self):
i = 0
string = '866260035710238'
while 1:
if i:
md5_str = hashlib.md5(string.encode('utf-8'))
self.payload["key"] = md5_str.hexdigest()
string = str(int(string) + 1)
await self.work_queue.put(self.payload.copy()) #必须要
i += 1
if i > self.max_loop:
break

async def run(self):
await self.producer()
print('start consumer...')

tasks = [
loop.create_task(self.consumer())
for i in range(10)
]

await self.work_queue.join()
print('end join')
for task in tasks:
task.cancel()

t1 = time.time()
loop = asyncio.get_event_loop()
test = Fetch()
loop.run_until_complete(test.run())
loop.close()
print(time.time() - t1)
shikanon wechat
欢迎您扫一扫,订阅我滴↑↑↑的微信公众号!