Can writing to a UDP socket ever block?(写入 UDP 套接字会阻塞吗?)
问题描述
如果是这样,在什么条件下?或者,换一种说法,在 twisted 中运行这段代码是否安全:
And if so, under what conditions? Or, phrased alternately, is it safe to run this code inside of twisted:
class StatsdClient(AbstractStatsdClient):
def __init__(self, host, port):
super(StatsdClient, self).__init__()
self.addr = (host, port)
self.server_hostname = socket.gethostname()
self.udp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def incr(self, stat, amount=1):
data = {"%s|c" % stat: amount}
self._send(data)
def _send(self, data):
for stat, value in data.iteritems():
self.udp_sock.sendto("servers.%s.%s:%s" % (self.server_hostname, stat, value), self.addr)
推荐答案
是的,奇怪的是,UDP 套接字可以阻塞.
Yes, oddly, a UDP socket can block.
发生这种情况的条件基本上是,某处的某些缓冲区已满,您的操作系统决定是时候阻止某些东西了.这些可以说是内核错误,但我到处都看到过它们.有时在晦涩难懂、无法重现的条件下,您绝对可以得到 EWOULDBLOCK
.
The conditions under which this can happen are basically, some buffers somewhere fill up, your operating system decides it's time for something to block. These are arguably kernel bugs, but I've seen them here and there. You can definitely get EWOULDBLOCK
sometimes under obscure, impossible-to-reproduce conditions.
为什么要在 Twisted 中执行此操作,而不是使用 Twisted 的内置 UDP 支持 虽然?
Why would you want to do this in Twisted instead of using Twisted's built-in UDP support though?
这篇关于写入 UDP 套接字会阻塞吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:写入 UDP 套接字会阻塞吗?


基础教程推荐
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01