gpt4 book ai didi

python - 在 multiprocessing.connection.Listener.accept() 的给定时间后引发 TimeOutError

转载 作者:行者123 更新时间:2023-11-28 17:14:32 25 4
gpt4 key购买 nike

我正在尝试中断 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com