[python-uk] how to kill the server in program

2006-02-15 Thread Yang, W (Wanjuan)
Hello,

I am building a server program. I have seen some examples like the following:

import pickle
import Queue
import socket
import threading
# We'll pickle a list of numbers, yet again:
someList = [ 1, 2, 7, 9, 0 ]
pickledList = pickle.dumps ( someList )
# A revised version of our thread class:
class ClientThread ( threading.Thread ):
# Note that we do not override Thread's __init__ method.
# The Queue module makes this not necessary.
def run ( self ):
# Have our thread serve "forever":
while True:
# Get a client out of the queue
client = clientPool.get()
# Check if we actually have an actual client in the client variable:
if client != None:
print 'Received connection:', client [ 1 ] [ 0 ]
client [ 0 ].send ( pickledList )
for x in xrange ( 10 ):
print client [ 0 ].recv ( 1024 )
client [ 0 ].close()
print 'Closed connection:', client [ 1 ] [ 0 ]
# Create our Queue:
clientPool = Queue.Queue ( 0 )
# Start two threads:
for x in xrange ( 2 ):
ClientThread().start()
# Set up the server:
server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '', 2727 ) )
server.listen ( 5 )
# Have the server serve "forever":
while True:
clientPool.put ( server.accept() )

Since the server main thread and child thread will run forever, my question is 
how can I exit the server nicely in the program when it is required (e.g no 
clients coming for a while) rather than using 'Ctrl-c' or kill the process. 

another question is: Is that when the main thread is killed, then the child 
thread will stop as well? In this case, before killing the main thread, we have 
to make sure the child thread already finish its job.  But how the main thread 
know whether the child thread finish the job or not. in other words, how does 
the main thread communicate the child thread? 

Anybody know? Thanks in advance!

Best Wishes,

Wendy Yang

___
python-uk mailing list
python-uk@python.org
http://mail.python.org/mailman/listinfo/python-uk


Re: [python-uk] how to kill the server in program

2006-02-15 Thread Chris Miles
On 15 Feb 2006, at 09:42, Yang, W (Wanjuan) wrote:
>
> Since the server main thread and child thread will run forever, my  
> question is how can I exit the server nicely in the program when it  
> is required (e.g no clients coming for a while) rather than using  
> 'Ctrl-c' or kill the process.

You can define signal handlers to clean things up and exit cleanly if  
a kill signal is sent to the process.  See http://docs.python.org/lib/ 
module-signal.html

> another question is: Is that when the main thread is killed, then  
> the child thread will stop as well? In this case, before killing  
> the main thread, we have to make sure the child thread already  
> finish its job.  But how the main thread know whether the child  
> thread finish the job or not. in other words, how does the main  
> thread communicate the child thread?

If you don't want the main thread to block on waiting for child  
threads to die before exiting cleanly, you can mark child threads as  
daemon threads using thread.setDaemon(1). See http://docs.python.org/ 
lib/thread-objects.html

If you don't want your main thread to die before the child threads  
have cleanly finished, then make sure to setDaemon(0) and you may  
also need to signal the child threads that they need to cleanup and  
exit.  A thread-safe method of doing this is to use a threading Event  
object.  Create one object and set it to true when you want all the  
children to exit.  The children need to periodically check the Event  
object and perform any cleanup if they find it set.  See http:// 
docs.python.org/lib/event-objects.html

Cheers,
CM

-- 
Chris Miles
http://chrismiles.info/

___
python-uk mailing list
python-uk@python.org
http://mail.python.org/mailman/listinfo/python-uk