How to have multi-threaded server log to one file ?

Stephen shriek at gmx.co.uk
Wed Feb 27 23:13:45 EST 2002


alanmk at hotmail.com (Alan Kennedy) wrote in message news:<f72dac0d.0202270140.97b925f at posting.google.com>...
> shriek at gmx.co.uk (Stephen) wrote in message 
> > When the server is run, each request from a client is handled by
> > MyHandler() in a separate thread. If multiple requests are being run
> > at the same time, how should I write my code so that they cal all
> > write to the same log file without conflict ?  
> 
> How about representing every log message as a simple object which is
> put() on a Queue.Queue? So each server thread produces messages to go
> on the Queue, and a single logging thread consumes those messages and
> does the actual writing into the log files. Since there is only ever
> one thread writing to the files, there are no contention problems.
> 
> That Queue.Queue class is mightily useful!

Thanks Alan (and Aahz) for pointing this out to me. 

I had a look at the effbot queue-example and think I understand the
Queue class but adapting my program to share the queue instance in the
first place is making my head spin.  Namely, how do I get the same
Queue object into both MyHandler and MyServer in the first place ?

Pseudo code showing where queue will be used ~
--------------------------------------------
 
class MyHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        # Do something interesting.
        # Log an error : 
        self.logerror("2002-02-28 12:00:01 Invalid argument") 
    
    def logerror(self, s):
        self.__queue.put(s)

class MyServer(SocketServer.ThreadingTCPServer):
    allow_reuse_address = 1
    queue = Queue.Queue(0) 
    self.__queue = queue
    
def start(host, port):
    server = MyServer((host, port), MyHandler)
    server.serve_forever() # serve forever


Or is this somewhere that a global variable Queue object should be
used ?

Stephen.



More information about the Python-list mailing list