Python 使用Asyncio执行HTTP请求
以下函数需要异步发送请求:Python 使用Asyncio执行HTTP请求,python,python-3.x,python-requests,python-asyncio,Python,Python 3.x,Python Requests,Python Asyncio,以下函数需要异步发送请求: async def checkin_post(payload, device_sn): payload = payload serial_number = device_sn print('\n' + 72 * '=' + '\nPOST /device/' + serial_number + '/check-in') resp = requests.post(base_url + '/device/' +
async def checkin_post(payload, device_sn):
payload = payload
serial_number = device_sn
print('\n' + 72 * '=' + '\nPOST /device/' +
serial_number + '/check-in')
resp = requests.post(base_url + '/device/' +
serial_number + '/check-in', auth=auth, json=payload)
return resp.status_code
async def main(payload, device_sn):
checkin_post(payload, device_sn)
devices.csv中的行数=100万
必需:每天发送100万行的POST请求,持续3天
代码可能会更改为:
async def checkin_post(payload, device_sn):
payload = payload
serial_number = device_sn
print('\n' + 72 * '=' + '\nPOST /device/' +
serial_number + '/check-in')
resp = requests.post(base_url + '/device/' +
serial_number + '/check-in', auth=auth, json=payload)
return resp.status_code
async def main(payload, device_sn):
checkin_post(payload, device_sn)
此外,由于没有
wait
,因此它不是真正的异步 如果您现在已经修好了,很抱歉回复晚了。
但您需要返回“确保未来对象”。
您可以尝试以下代码:
async def check_in():
logging.info('Starting')
count = 0
futures = []
while count < 3:
logging.info('Check-in Day = ' + str(count))
with open('devices.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
device_sn = row['serial_number']
payload = {
"product": "##",
"board": "###",
"hardware_id": "0000",
"usage_id": "000",
"mac_address": row['mac_address'],
"serial_number": row['serial_number']
}
logging.info(
'Check-in device: ' + device_sn)
async with aiohttp.ClientSession() as session:
async with session.post(base_url + '/device/' + device_sn + '/check-in', auth=auth, json=payload) as resp:
futures.append(asyncio.ensure_future(await resp.status))
count += 1
return await asyncio.gather(*futures)
def main():
loop = asyncio.get_event_loop()
futures = asyncio.ensure_future(check_in())
responses = loop.run_until_complete(futures)
async def check_in():
logging.info('开始')
计数=0
期货=[]
当计数小于3时:
logging.info('Check-in Day='+str(count))
打开('devices.csv',换行符='')作为csvfile:
reader=csv.DictReader(csvfile)
对于读取器中的行:
设备编号=行[“序列号”]
有效载荷={
“产品”:“##”,
“董事会”:“董事会”,
“硬件id”:“0000”,
“使用标识”:“000”,
“mac_地址”:行['mac_地址],
“序列号”:行[“序列号”]
}
logging.info(
“签入设备:”+设备编号)
与aiohttp.ClientSession()作为会话异步:
与session.post(base_url+'/device/'+device_sn+'/check-in',auth=auth,json=payload)异步,分别为:
futures.append(异步确保未来(等待响应状态))
计数+=1
return wait asyncio.gather(*futures)
def main():
loop=asyncio.get\u event\u loop()
futures=asyncio.确保未来(签入())
响应=循环。运行直到完成(未来)
目前这个问题太广泛了。您是否自己尝试过将其从请求转换为asyncio?@wpercy我已经发布了我尝试过的内容,我确信它不正确。您要么在线程池中执行对请求的调用,要么查看aiohttpI是否更新了我的问题,负载和序列号是否从另一个函数传递。您是否尝试过删除打印(resp.status)语句?也许你可以在等待回复之前打印状态。请看下面答案中的我的试用。这也不起作用。请编辑您的原始问题,而不是发布新答案:@jtbandes感谢您的审阅。编辑了我的原始答案。
async def check_in():
logging.info('Starting')
count = 0
futures = []
while count < 3:
logging.info('Check-in Day = ' + str(count))
with open('devices.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
device_sn = row['serial_number']
payload = {
"product": "##",
"board": "###",
"hardware_id": "0000",
"usage_id": "000",
"mac_address": row['mac_address'],
"serial_number": row['serial_number']
}
logging.info(
'Check-in device: ' + device_sn)
async with aiohttp.ClientSession() as session:
async with session.post(base_url + '/device/' + device_sn + '/check-in', auth=auth, json=payload) as resp:
futures.append(asyncio.ensure_future(await resp.status))
count += 1
return await asyncio.gather(*futures)
def main():
loop = asyncio.get_event_loop()
futures = asyncio.ensure_future(check_in())
responses = loop.run_until_complete(futures)