写入 UDP 套接字会阻塞吗?

Can writing to a UDP socket ever block?(写入 UDP 套接字会阻塞吗?)
本文介绍了写入 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 套接字会阻塞吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)