- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试中断 multiprocessing.connection.Listener.accept()
,但迄今为止没有成功。由于它不提供 timeout
参数,我想也许我可以使用 socket.setdefaulttimeout()
来中断它,正如我再也找不到的帖子中所建议的那样,在这里所以。
这没有用。然后,我尝试在 Listener()
对象上调用 close()
。根据 this post's answer ,这应该有效。
然而,这些对象似乎无法与通常的 socket
相关解决方案一起使用。
我可以确认 Listener
已按预期被 Timer
对象关闭,但 accept()
调用未中断.
代码:
import logging
import socket
import os
from multiprocessing.connection import Listener
from queue import Queue, Empty
from threading import Thread, Event, Timer
class Node(Thread):
"""Base Class providing a AF_INET, AF_UNIX or AF_PIPE connection to its
data queue. It offers put() and get() method wrappers, and therefore
behaves like a Queue as well as a Thread.
Data from the internal queue is automatically fed to any connecting client.
"""
def __init__(self, sock_name, max_q_size=None, timeout=None,
*thread_args, **thread_kwargs):
"""Initialize class.
:param sock_name: UDS, TCP socket or pipe name
:param max_q_size: maximum queue size for self.q, default infinite
"""
self._sock_name = sock_name
self.connector = Listener(sock_name)
max_q_size = max_q_size if max_q_size else 0
self.q = Queue(maxsize=max_q_size)
self._running = Event()
self.connection_timer = Timer(timeout, self.connection_timed_out)
super(Node, self).__init__(*thread_args, **thread_kwargs)
def connection_timed_out(self):
"""Closes the Listener and shuts down Node if no Client connected.
:return:
"""
self.connector.close()
self.join()
def _start_connection_timer(self):
self.connection_timer.start()
def start(self):
self._running.set()
super(Node, self).start()
def join(self, timeout=None):
print("clearing..")
self._running.clear()
print("internal join")
super(Node, self).join(timeout=timeout)
print("Done")
def run(self):
while self._running.is_set():
print("Accepting connections..")
self._start_connection_timer()
try:
client = self.connector.accept()
self.connection_timer.cancel()
self.feed_data(client)
except (TimeoutError, socket.timeout):
continue
except Exception as e:
raise
print("Run() Terminated!")
def feed_data(self, client):
try:
while self._running.is_set():
try:
client.send(self.q.get())
except Empty:
continue
except EOFError:
return
if __name__ == '__main__':
import time
n = Node('/home/nils/git/spab2/test.uds', timeout=10)
n.start()
print("Sleeping")
time.sleep(15)
print("Manual join")
n.join()
我意识到我的问题与此 question 重复- 然而,它已经快一年了,甚至还没有收到评论。此外,我使用的是 Unix Domain Socket
,而不是链接帖子的 TCP
连接。
最佳答案
我设法在 Python 2.7
中以下列方式设置超时:
self.listener = mpc.Listener((address, port))
self.listener._listener._socket.settimeout(3)
这样,accept()
调用就被中断了。
结果:
conn = self.listener.accept()
File "/usr/lib/python2.7/multiprocessing/connection.py", line 145, in accept
c = self._listener.accept()
File "/usr/lib/python2.7/multiprocessing/connection.py", line 275, in accept
s, self._last_accepted = self._socket.accept()
File "/usr/lib/python2.7/socket.py", line 202, in accept
sock, addr = self._sock.accept()
timeout: timed out
问候,亨利
关于python - 在 multiprocessing.connection.Listener.accept() 的给定时间后引发 TimeOutError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45166547/
在 HTTP 中,您可以在请求中指定您的客户端可以使用 accept header 接受响应中的特定内容,其值如 application/xml。内容类型规范允许您在内容类型中包含参数,例如chars
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
我的 RESTFul API 只能响应 JSON 编码的数据(即我所有的 header 都有 Content-Type: application/json)。如果请求具有不允许 JSON 的 Acce
我有 C linux TCP 客户端/服务器应用程序。我想出了一个奇怪的场景,但我不知道这个应用程序是否有任何后果。我有一个可以接受 N 个连接的服务器端,例如这个服务器将接受 100 个连接。在这种
我试图找出 的可能值范围接受 和 接受-联系 header 字段,但我在 RFC 中找不到完整列表。有谁知道他们在哪里?我经常看到 Accept: application/sdp;level=1,
在 TCP 套接字代码中,我们创建了 2 个套接字。第一个接受新连接,第二个接受来自客户端的数据并在新连接建立时创建。 TCP header 中的哪个控制位允许服务器知道将此段传递给 ServerSo
我可以设置 Request.Content-Type = ... , Request.Content-Length = ... 如何设置Accept和Accept-Language? 我想上传一个文件
假设A是服务器,B是客户端。 B 创建一个到服务器 A 的套接字 b 并 ServerSocket.accept() 创建一个相应的套接字 a 对于客户端 B 现在,如果服务器 A 关闭了 a 但客户
如果客户端connect()先于服务端accept()成功,那么客户端如何知道服务端accept()成功呢?如果在 connect() 成功和服务器 accept() 之间的时间间隔调用 send()
这是我的代码: void error(const char *msg) { perror(msg); exit(1); } void sServer::acceptClientConn
在我的 pom.xml 中有以下详细信息 org.codehaus.jackson jackson-core-asl 1.9.13
假设我有两个如下所示的端点: @GET @Path("/blah") @Produces(MIME_TYPE_1) public Thing getThing() { .... } @GET
我的 Controller : @RestController public class ClawerController { @RequestMapping("/hello"
我不想要可视化 merge 工具,我也不希望必须 vi 冲突文件并手动在 HEAD(我的)和导入的更改(他们的)之间进行选择。大多数时候,我要么想要他们的所有更改,要么想要我的所有更改。通常这是因为我
我正在使用 spring 4.1.1.RELEASE 并包括: pom 中的 jackson-core-asl 1.9.13 和 jackson-mapper-asl 1.9.13 来创建一个带有 R
我正在尝试开发自己的基于非阻塞 NIO 消息的通信库。我已经阅读了 1000 个关于它的教程和书籍章节,我认为最终我得到了一些可以在很少的同时连接下工作的东西。但是当我在服务器端共存许多连接时,我遇到
我正在尝试编写一个 Bind 元编程模板辅助元函数,将模板参数绑定(bind)到某物。 我有一个简单的模板元函数的工作实现: template struct MakePair { using
我使用 spring 构建 Restful API,当我访问以下方法时: // get the entity in DB by using id number @RequestMapping(
只是想知道是否有一种方法可以通过 ASP.NET Core 中的属性强制检查 HTTP header 值是否属于一组给定值(例如 application/json,应用程序/xml) 我知道有 Pro
我是一名优秀的程序员,十分优秀!