GC 期间 Win UDP 套接字出现奇怪的延迟
Strange delay on Win UDP socket during GC
关闭之前发送到我的路由器已知主机的 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。
这似乎是一种 Windows 主义。我在 Python 2.7 使用不在网络上的 IP 地址时发现了同样的问题。我还可以使用 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
关闭之前发送到我的路由器已知主机的 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。
这似乎是一种 Windows 主义。我在 Python 2.7 使用不在网络上的 IP 地址时发现了同样的问题。我还可以使用 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