GC期间Win UDP套接字的奇怪延迟

时间:2015-03-29 21:33:10

标签: python performance sockets garbage-collection delay

当关闭先前发送到路由器已知主机的UDP套接字时,需要永久关闭。发生了什么,我该怎样绕过?

$ python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket,time
>>> def strange_delay(host):
...     s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
...     s.sendto(b'whatever', (host,80))
...     t = time.perf_counter()
...     s = None        # This will take a huge amount of time. GC@work?
...     print('Strange delay is', time.perf_counter()-t)
...
>>> strange_delay('google.com')
Strange delay is 0.0003058695615487909
>>> strange_delay('hostname-known-to-my-router-but-currently-offline')
Strange delay is 2.5981389268589035

我在Python 2上看到完全相同的东西。它总是发生在Windows上,它永远不会发生在Linux上。

1 个答案:

答案 0 :(得分:1)

这似乎是Windows主义。我发现使用不在网络上的IP地址的Python 2.7也存在同样的问题。我也可以使用Java复制问题。这篇文章似乎很好地描述了这个问题: http://blogs.msdn.com/b/winsdk/archive/2013/10/09/udp-closesocket-takes-upto-5-seconds-to-return-in-disconnect-remote-host-down-scenario-due-to-pending-data-to-send.aspx