Reputation: 118560
I've written a replacement threading
module for Python. What's the best method to go about hooking all uses of the usual threading
module from the standard library with my own? The hooking should be opt-in, and on a per project, or per executable basis.
When this statement is executed:
import threading
I want my module to be loaded instead of the default.
Note that in future, I may also hook several of the other standard modules, so a solution that addresses hooking a few modules is best.
I've implemented IO concurrency via greenlets and Linux's eventfd()
and epoll()
system calls. It works transparently except for a sys.modules
hook and a replacement socket
class. Looking to make this hooking nicer and more consistent.
Upvotes: 1
Views: 407
Reputation: 45039
You might try using python's meta_path hook:
http://www.python.org/dev/peps/pep-0302/
It'll allow you add an object which can override part of the importing process. I think you'll be able to substitute your own modules for the standard modules with that way. I'm not sure its better then patching sys.modules though.
Upvotes: 3
Reputation: 71495
Can you not just put your replacement threading
module in some directory, and make sure that directory is listed in PYTHONPATH
when you want to use it? That should mean that directory is searched before the default places when importing threading
, and Python will find your module and stop.
Upvotes: 3