How make your module substitute a python stdlib module.

Mark Bourne nntp.mbourne at spamgourmet.com
Tue Dec 27 08:37:08 EST 2022


Antoon Pardon wrote:
> 
> 
> Op 27/12/2022 om 11:37 schreef Chris Angelico:
>> On Tue, 27 Dec 2022 at 21:29, Antoon Pardon <antoon.pardon at vub.be> wrote:
>>> OK, I am writing an alternative for the threading module. What I would
>>> like to know is how I can get some library modules call my alternative
>>> instead of the threading module.
>>>
>>> For instance there is the logging module, it can log the thread name. So
>>> I would like to know how I can get the logging module to call the
>>> function from my module to get the current_thread, instead of it calling
>>> "current_thread" from the threading module.
>> Easy: make sure your module is called "threading.py" and is earlier in
>> the path than the standard library. In fact, it's so easy that people
>> do it unintentionally all the time... Generally, the current directory
>> (or the script directory) is the first entry in sys.path, so that's a
>> good place to put it.
> 
> Well I had hope for a somewhat more selective solution. The intention is
> to have a number of modules collected in a package where this module is
> one of and the package is available via the "installed" search path.
> 
> So the programmer should just be able to choose to write his code with
> either:
> 
>      from threading import Thread
> 
> or
> 
>      from QYZlib.threaders import Thread 

In that case, it sounds like you don't want to always replace the 
logging module's behaviour, since code might use the standard threading 
module.  You might also need to consider that a mix of both the standard 
threading module and yours might be used (particularly if using other 
libraries which create their own threads using the standard module).

It might be better for your module to provide a custom `logging.Filter` 
subclass, which replaces the `thread` and `threadName` attributes of the 
`LogRecord` with your threading module's idea of the thread ID.  Then 
applications using your threading module would configure the `logging` 
module to use your filter, while code using the standard threading 
module can continue as normal.  Despite the name, logging filters can 
also modify log records, not just filter whether or not they're logged.

It might even be desirable to leave the original `thread` and 
`threadName` attributes unchanged and add new attributes to the 
`LogRecord` for your module's idea of threads (e.g. `myThread` and 
`myThreadName`).  I think using `%(myThread)d` and `%(myThreadName)s` in 
the log format string would use those attributes, without needing a 
custom formatter.  That would allow both thread IDs to be logged, in 
case a mix of standard threads and your threads is used.

-- 
Mark.


More information about the Python-list mailing list