请求,无法分配请求的地址,超出端口? [英] requests, cannot assign requested address, out of ports?

查看:30
本文介绍了请求,无法分配请求的地址,超出端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

requests.exceptions.ConnectionError: ('Connection aborted.', error(99, 'Cannot assign requested address'))

在使用 python 请求库运行多个进程并将 post 函数调用到返回非常快(<10 毫秒)的 API 时,我收到此错误.

I was getting this error when running multiple processes utilizing the python requests library and calling the post function to an API that was returning very quickly (<10ms).

减少正在运行的进程数有延迟效果,但只减少到 1 个进程就可以解决问题.这不是解决方案,但确实表明有限的资源是罪魁祸首.

Dialing down the number of processes running had a delaying effect, but only dialing down to 1 process eliminated the problem. This was not a solution, but did indicate a finite resource was the culprit.

推荐答案

我解决问题的方法是使用 requests.Session 类,在该类中我会为每次调用重用相同的连接/会话在给定的过程中.

The way I resolved my issue was to use the requests.Session class where I would reuse the same connection/session for each call in a given process.

人为的例子:

import requests
for line in file:
  requests.get('http://example.com/api?key={key}'.format(key=line['key']))

变成

import requests
with requests.Session() as session:
  for line in file:
    session.get('http://example.com/api?key={key}'.format(key=line['key']))

这些问题有一些相关的建议:

These questions had some related advice:

重复POST 请求导致错误socket.error: (99, '无法分配请求的地址')"Python urllib2:无法分配请求的地址

这篇关于请求,无法分配请求的地址,超出端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆