赞
踩
原文地址
在原先的基础上使用协程进行改进,提高效率
async def fetch(session, url, headers, payload, delay=0):
await asyncio.sleep(delay)
async with session.post(url, headers=headers, data=payload, ssl=False) as response:
return await response.text()
async def main(url, headers, payload, n, delay):
d = defaultdict(int)
lock = asyncio.Lock() # 创建锁
async with aiohttp.ClientSession() as session:
tasks = [fetch_and_update(session, url, headers, payload, d, lock, delay) for _ in range(n)]
await asyncio.gather(*tasks)
return d
if __name__ == '__main__':
url = ''
headers = ''
payload = ''
n = 100000
delay = 0.1 # 单位是s
loop = asyncio.get_event_loop()
result = loop.run_until_complete(main(url, headers, payload, n, delay))
get_lottery(result)
由于是对同一个字典进行操作,需要保证操作的线程安全性,保证数据一致性,需要加锁处理 ,使用asyncio.Lock
async def fetch_and_update(session, url, headers, payload, d, lock, delay):
response = await fetch(session, url, headers, payload, delay)
jsonobj = json.loads(response)
reward_name = jsonobj['']
async with lock: # 确保对字典的访问是线程安全的
d[reward_name] += 1
def get_lottery(d):
for key, value in d.items():
lottery = value / n
print(key + "的概率是:" + '{:.2%}'.format(lottery))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。