PEP 282: A Logging System

Vinay Sajip vinay_sajip at yahoo.co.uk
Fri Apr 12 04:01:57 EDT 2002


aahz at pythoncraft.com (Aahz) wrote in message news:<a95c7k$801$1 at panix1.panix.com>...
> Your main difficulty with thread safety is WRT I/O.  How do you plan to
> handle that?

I/O is the province of the handlers, and I expect to make them
thread-safe with respect to the underlying I/O through the use of
thread locks. Of course this applies only to the handlers I provide as
part of the distribution; user-defined handlers will need to make
their own provision.

The code released up till now does not have any locking at all.
Empirically, a simple test harness I put together (just to see how the
current code does) seems to run fine (eyeball-wise) on Python
2.1.1/Windows and Python 1.5.2/Windows and Linux, though with 1.5.2 I
had to remove calls to inspect. The test was logging to a FileHandler
and a DatagramHandler. I was a little surprised at first not to see
any problems, might this be due to the GIL? I don't know enough in
detail about how the GIL works, but given that these handlers all work
through C objects I would expect the GIL to play a role.

I think that only the emit() method of handlers needs to be
synchronized - that's where the I/O happens. I expect to do something
like this:

class Handler(Filterer):
  def __init__(self, ...):
    ...
    self.getlock()
		
  def getlock(self):
    if thread:
      self.lock = thread.allocate_lock()
    else:
      self.lock = None
	
  def acquire(self):
    if self.lock:
      self.lock.acquire()
			
  def release(self):
    if self.lock:
      self.lock.release()
	
Then, in individual handlers...

class MyHandler(Handler):
  ...
  def emit(self, record):
    self.acquire()
    try:
      try:
        ... here, do the actual emission
      except:
        ... silently deal with exceptions during emission
    finally:
      self.release()
			
If anybody reading this spots any flaws in the above, please do let me
know :-)

Regards


Vinay Sajip



More information about the Python-list mailing list