how do I stop SocketServer()?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandru  Mosoi

    #1

    how do I stop SocketServer()?

    supposing that I have a server (an instance of SocketServer()) that
    waits for a connection (ie is blocked in accept()) and in another
    thread i want to stop the server, how do I do that?
  • Diez B. Roggisch

    #2
    Re: how do I stop SocketServer()?

    Alexandru Mosoi wrote:
    supposing that I have a server (an instance of SocketServer()) that
    waits for a connection (ie is blocked in accept()) and in another
    thread i want to stop the server, how do I do that?
    By setting a timeout on the socket using socket.settimeo ut, and then
    periodically check for an abortion condition in the server thread before
    re-accepting connections.

    Diez

    Comment

    • Uberman

      #3
      Re: how do I stop SocketServer()?

      On Wed, 27 Aug 2008 18:44:46 +0200, "Diez B. Roggisch"
      <deets@nospam.w eb.dewrote:
      >Alexandru Mosoi wrote:
      >
      >supposing that I have a server (an instance of SocketServer()) that
      >waits for a connection (ie is blocked in accept()) and in another
      >thread i want to stop the server, how do I do that?
      >
      >By setting a timeout on the socket using socket.settimeo ut, and then
      >periodically check for an abortion condition in the server thread before
      >re-accepting connections.
      You can also poll for activity by using the select() call. For
      example:

      ...
      local_host = '' # Symbolic name meaning the local host

      server_port = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      server_port.bin d((local_host, local_port))
      server_port.lis ten(1)

      readsocks = []
      readsocks.appen d(sync_port)

      # 'halt' would be your stop condition, set elsewhere
      while halt == False:
      readables, writeables, exceptions = select(readsock s, [], [], 1)

      # does the socket has a connection pending?
      if server_port in readables:
      # process client connection
      client_conn, client_addr = server_port.acc ept()
      ...

      Comment

      Working...