【发布时间】:2017-06-01 02:55:33
【问题描述】:
我有一个多线程 python 程序,它会不断打开与本地 URL 的连接(http://testing.com/sendMessages_mock.php),我已经安装了 WAMP 并在本地有 sendMessages_mock.php 文件用于测试目的。有时,我得到“[Errno 10061] 无法建立连接,因为目标机器主动拒绝它”
每个线程都会调用这个函数。每一秒,就会有数百个线程调用这个函数。仅针对少数线程调用获取此异常。对此有什么想法吗?
def sendMessage(body):
try:
request = Request(url="http://testing.com/sendMessages_mock.php", data=json.dumps(body))
request.add_header('Authorization', 'key=' + CONST.API_KEY)
request.add_header('Content-Type', 'application/json')
response = urlopen(request)
responseData = response.read().decode('utf-8')
except Exception as exceptionErr:
print("Oops! " + str(traceback.format_exc()))
【问题讨论】:
-
服务器端侦听连接,但
listen调用有一个积压 - 尚未接受的传入连接请求的最大数量。当该请求队列太大时,服务器 TCP 堆栈可能会开始拒绝连接。这可能就是你所看到的。您可能可以将服务器配置为更大的积压,但也要考虑到这是正常的背压,告诉您的客户冷静一点。
标签: python multithreading python-2.7 python-multithreading urlopen