python套接字对象接受超时

时间:2021-01-18 23:56:02

Question: Is there some sort of time out or interrupt to the socket.accept() function in python?

问题:python中的socket.accept()函数是否有某种超时或中断?


Info:

信息:

I have a program that has a child thread bound to a port and constantly accepting and tending and passing them to a queue for the main thread. Right now I'm trying to get the child thread to interrupt so it can deconstruct appropriately. I think it is possible for me to just simply stop the child thread and have the parent deconstruct the child, but there are other times where I want to be able to return early form accept so I just decided that would be the most useful approach.

我有一个程序,它有一个绑定到端口的子线程,并不断接受和管理并将它们传递给主线程的队列。现在我正试图让子线程中断,以便它可以适当地解构。我认为我可以简单地停止子线程并让父母解构孩子,但是有时候我希望能够提前返回接受,所以我决定这将是最有用的方法。

So, is there a way that I can have a time out or cancel the accept method so the thread can return w/o having something connect to it first?

那么,有没有一种方法可以让我有一个超时或取消接受方法,这样线程可以返回没有先连接到它的东西?

3 个解决方案

#1


8  

You can set the default timeout with

您可以使用设置默认超时

import socket
print socket.getdefaulttimeout()
socket.setdefaulttimeout(60)

AFAIK This will affect all the socket operation

AFAIK这将影响所有套接字操作

#2


6  

Maybe settimeout() is what you're looking for.

也许settimeout()正是你要找的。

#3


3  

You can use settimeout() as in this example:

您可以使用settimeout(),如下例所示:

import socket

tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
tcpServer.bind(('0.0.0.0', 10000)) # IP and PORT

stopped = False
while not stopped:
  try:
    tcpServer.settimeout(0.2) # timeout for listening
    tcpServer.listen(1) 
    (conn, (ip, port)) = tcpServer.accept() 
  except socket.timeout:
    pass
  except:
    raise
  else:
    # work with the connection, create a thread etc.
    ...

The loop will run until stopped is set to true and then exit after (at most) the timeout you have set. (In my application I pass the connection handle to a newly created thread and continue the loop in order to be able to accept further simultaneous connections.)

循环将一直运行,直到stopped设置为true,然后在(最多)您设置的超时后退出。 (在我的应用程序中,我将连接句柄传递给新创建的线程并继续循环,以便能够接受进一步的同时连接。)

#1


8  

You can set the default timeout with

您可以使用设置默认超时

import socket
print socket.getdefaulttimeout()
socket.setdefaulttimeout(60)

AFAIK This will affect all the socket operation

AFAIK这将影响所有套接字操作

#2


6  

Maybe settimeout() is what you're looking for.

也许settimeout()正是你要找的。

#3


3  

You can use settimeout() as in this example:

您可以使用settimeout(),如下例所示:

import socket

tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
tcpServer.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 
tcpServer.bind(('0.0.0.0', 10000)) # IP and PORT

stopped = False
while not stopped:
  try:
    tcpServer.settimeout(0.2) # timeout for listening
    tcpServer.listen(1) 
    (conn, (ip, port)) = tcpServer.accept() 
  except socket.timeout:
    pass
  except:
    raise
  else:
    # work with the connection, create a thread etc.
    ...

The loop will run until stopped is set to true and then exit after (at most) the timeout you have set. (In my application I pass the connection handle to a newly created thread and continue the loop in order to be able to accept further simultaneous connections.)

循环将一直运行,直到stopped设置为true,然后在(最多)您设置的超时后退出。 (在我的应用程序中,我将连接句柄传递给新创建的线程并继续循环,以便能够接受进一步的同时连接。)