Returning from socket.accept and threading issues.

  • Thread starter Christopher J. Bottaro
  • Start date
C

Christopher J. Bottaro

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.
 
P

Peter Hansen

Christopher said:
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.

Would calling setDaemon() on the child threads not be sufficient?

(I'm unsure of the capitalization of that... check the docs or use
dir() to confirm what it's called.)

-Peter
 
P

Paul Clinch

Christopher J. Bottaro said:
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.

If you call select with a short timeout eg:

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

it uses almost no CPU.
Thanks for the help.

Regards, Paul Clinch
 
E

Elbert Lev

Christopher J. Bottaro said:
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.


Look here:

http://groups.google.com/[email protected]#link4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top