Python 3.x Python套接字.gethostbyaddr()不响应SIGINT

Python 3.x Python套接字.gethostbyaddr()不响应SIGINT,python-3.x,Python 3.x,运行约20秒,但失败。在这20秒内的任何时候发送SIGINT(control-C)都会导致命令立即结束 另一方面,在Python 3.6.4中: $ nslookup 192.168.85.242 点击control-C没有任何作用,socket命令似乎决心不放弃 我的问题是,为什么?最重要的是,如果我在多个IP地址之间循环,并且我希望我的代码在2秒钟后失败,那么实现超时(如And)的标准方法将不起作用。尝试Ctrl+\,它将发出SIGQUIT而不是SIGINT。当CTRL+C失败时,它往往会

运行约20秒,但失败。在这20秒内的任何时候发送SIGINT(control-C)都会导致命令立即结束

另一方面,在Python 3.6.4中:

$ nslookup 192.168.85.242
点击control-C没有任何作用,socket命令似乎决心不放弃


我的问题是,为什么?最重要的是,如果我在多个IP地址之间循环,并且我希望我的代码在2秒钟后失败,那么实现超时(如And)的标准方法将不起作用。

尝试
Ctrl+\
,它将发出
SIGQUIT
而不是
SIGINT
。当
CTRL+C
失败时,它往往会终止进程。

我最终这样做了,出于某种原因,多处理模块具有使gethostbyaddr()方法超时的“能力”

在我的主要代码中,我有:

@functools.lru_cache(maxsize=None)
def gethostbyaddr(ip_address):
    try:
        base.get_logger().debug(f"Looking up {ip_address} ...")
        hostname, _, _ = socket.gethostbyaddr(ip_address)
        return hostname
    except (Exception, ) as e:
        base.get_logger().debug(str(e) + " for " + ip_address)
        return None

# We need this pool/multiprocessing business so as to timeout after a couple of seconds
# if we are not going to get an answer
# See https://docs.python.org/3/library/multiprocessing.html#using-a-pool-of-workers
@functools.lru_cache(maxsize=None)
def get_host_name(ip_address):
    with multiprocessing.Pool(processes=1) as pool:
        try:
            result = pool.apply_async(gethostbyaddr, (ip_address, ))
            return result.get(timeout=2)
        except multiprocessing.context.TimeoutError:
            return None
@functools.lru_cache(maxsize=None)
def gethostbyaddr(ip_address):
    try:
        base.get_logger().debug(f"Looking up {ip_address} ...")
        hostname, _, _ = socket.gethostbyaddr(ip_address)
        return hostname
    except (Exception, ) as e:
        base.get_logger().debug(str(e) + " for " + ip_address)
        return None

# We need this pool/multiprocessing business so as to timeout after a couple of seconds
# if we are not going to get an answer
# See https://docs.python.org/3/library/multiprocessing.html#using-a-pool-of-workers
@functools.lru_cache(maxsize=None)
def get_host_name(ip_address):
    with multiprocessing.Pool(processes=1) as pool:
        try:
            result = pool.apply_async(gethostbyaddr, (ip_address, ))
            return result.get(timeout=2)
        except multiprocessing.context.TimeoutError:
            return None
entry_dict[defs.DNS] = get_host_name(row[defs.IP])