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

Stephen shriek at gmx.co.uk
Fri Mar 1 04:47:26 CET 2002


> > [...] how do I get the same Queue object into both MyHandler and
> > MyServer in the first place ? [...] Or is this somewhere that a
> > global variable Queue object should be used ?
> 
> Try making a separate module that says:
> 
> ---------------------------------------------------------------
> # logging.py - used by everyone else that needs to log messages
> 
> import Queue
> 
> # This is the queue object that's used to hold log events.
> # LoggingThread knows about it, and the log() function below
> # knows about it.  No one else is allowed to see it.
> _log_queue = Queue.Queue()
> 
> class LoggingThread:
>     ...
>     def run(self):
>         while 1:
>             message = _log_queue.get()
>             self.logfile.write(message + '\n')  # (or whatever)
> 
> # Kick off the logging thread.
> LoggingThread().start()
> 
> def log(msg):
>     _log_queue.put(msg)
> 
> ---------------------------------------------------------------
> 
> Then your MyHandler and MyServer code can just use the log()
> function, like this:
> 
>   from logging import log
>   ...
>   ...
>   log("Something happened")
>   ...
> 
> ## Jason Orendorff    http://www.jorendorff.com/

(I thought I'd posted a reply to this but it seems to have got lost.)

Thank you Jason, this is such a clear and simple example. It's never
occured to me to create the object within the module (in your example,
the Queue.Queue instance "_log_queue" within the "log" module) and
then access it indirectly through a function (ie. log.log()). I
usually end up passing objects around in my code but the method you
describe looks like an incredibly good way to avoid this as well as
hide complexity.

This type of code (having shared objects instantiated within an
imported module) looks like something I should employ more often.
One of them 'ta-da' moments.

Stephen.



More information about the Python-list mailing list