Returning from socket.accept and threading issues.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher J. Bottaro

    #1

    Returning from socket.accept and threading issues.

    Hello,
    I'm trying to write a fairly simple network program. The main thread spawns
    a thread which creates a listener socket and then calls socket.accept on
    it. socket.accept blocks indefinantly. My problem is that when the main
    thread determines that it is time to quit, how do I get the spawned thread
    to exit? Preferably I'd like the spawned thread to return from
    socket.accept when the main thread tells it to and then check a shared
    varible to determine if it should die or call socket.accept again.

    Any advice on this stuff would be greatly appreciated.

    My temporary solution is to poll (via select.select) the listening socket
    for input before socket.accept is called. I don't like this method because
    it eats up CPU time.

    Thanks for the help.

  • Paul Clinch

    #2
    Re: Returning from socket.accept and threading issues.

    "Christophe r J. Bottaro" <cjbottaro@alum ni.cs.utexas.ed u> wrote in message news:<mailman.5 189.1098211519. 5135.python-list@python.org >...[color=blue]
    > Hello,
    > I'm trying to write a fairly simple network program. The main thread spawns
    > a thread which creates a listener socket and then calls socket.accept on
    > it. socket.accept blocks indefinantly. My problem is that when the main
    > thread determines that it is time to quit, how do I get the spawned thread
    > to exit? Preferably I'd like the spawned thread to return from
    > socket.accept when the main thread tells it to and then check a shared
    > varible to determine if it should die or call socket.accept again.
    >
    > Any advice on this stuff would be greatly appreciated.
    >
    > My temporary solution is to poll (via select.select) the listening socket
    > for input before socket.accept is called. I don't like this method because
    > it eats up CPU time.[/color]

    If you call select with a short timeout eg:

    select.select( [], [], [], 2.0 )

    it uses almost no CPU.
    [color=blue]
    > Thanks for the help.[/color]

    Regards, Paul Clinch

    Comment

    • Elbert Lev

      #3
      Re: Returning from socket.accept and threading issues.

      "Christophe r J. Bottaro" <cjbottaro@alum ni.cs.utexas.ed u> wrote in message news:<mailman.5 189.1098211519. 5135.python-list@python.org >...[color=blue]
      > Hello,
      > I'm trying to write a fairly simple network program. The main thread spawns
      > a thread which creates a listener socket and then calls socket.accept on
      > it. socket.accept blocks indefinantly. My problem is that when the main
      > thread determines that it is time to quit, how do I get the spawned thread
      > to exit? Preferably I'd like the spawned thread to return from
      > socket.accept when the main thread tells it to and then check a shared
      > varible to determine if it should die or call socket.accept again.
      >
      > Any advice on this stuff would be greatly appreciated.
      >
      > My temporary solution is to poll (via select.select) the listening socket
      > for input before socket.accept is called. I don't like this method because
      > it eats up CPU time.
      >
      > Thanks for the help.[/color]


      Look here:


      Comment

      Working...