On Tue, 27 Dec 2022 at 21:29, Antoon Pardon <antoon.pardon@vub.be> wrote:
OK, I am writing an alternative for the threading module. What I wouldEasy: make sure your module is called "threading.py" and is earlier in
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.
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.
like to know is how I can get some library modules call my alternative >instead of the threading module.
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.
Op 27/12/2022 om 11:37 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 21:29, Antoon Pardon <antoon.pardon@vub.be> wrote:
OK, I am writing an alternative for the threading module. What I wouldEasy: make sure your module is called "threading.py" and is earlier in
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.
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
On Tue, 27 Dec 2022 at 22:13, Antoon Pardon<antoon.pardon@vub.be> wrote:
How do you intend to distinguish one from the other? How should the
Op 27/12/2022 om 11:37 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 21:29, Antoon Pardon<antoon.pardon@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.Well I had hope for a somewhat more selective solution. The intention is
Easy: make sure your module is called "threading.py" and is earlier in
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.
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.
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
logging module know which threading module to use?
How do you intend to distinguish one from the other? How should the
logging module know which threading module to use?
That is my question! How can I get the logging module to use my module.I was hoping the logging module would allow some kind of dependency
injection, so you can tell it what threading module to use. An other
option might be to manipulate sys.modules. -- Antoon Pardon
On Tue, 27 Dec 2022 at 23:06, Antoon Pardon<antoon.pardon@vub.be> wrote:
But presumably you want OTHER modules to continue using the vanillaHow do you intend to distinguish one from the other? How should theThat is my question! How can I get the logging module to use my module.I was hoping the logging module would allow some kind of dependency
logging module know which threading module to use?
injection, so you can tell it what threading module to use. An other
option might be to manipulate sys.modules. -- Antoon Pardon
threading module? This is likely to end up somewhat hacky. Yes, you
can manipulate sys.modules, but there's only one threading module at a
time, so you need a way to distinguish between modules that should use
the changed one and modules that shouldn't.
Op 27/12/2022 om 13:09 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 23:06, Antoon Pardon<antoon.pardon@vub.be> wrote:
But presumably you want OTHER modules to continue using the vanilla threading module? This is likely to end up somewhat hacky. Yes, youHow do you intend to distinguish one from the other? How should theThat is my question! How can I get the logging module to use my module.I was hoping the logging module would allow some kind of dependency
logging module know which threading module to use?
injection, so you can tell it what threading module to use. An other
option might be to manipulate sys.modules. -- Antoon Pardon
can manipulate sys.modules, but there's only one threading module at a time, so you need a way to distinguish between modules that should use
the changed one and modules that shouldn't.
But don't these caveats also apply with your original answer of having a local threading.py file?
At the moment I am happy with a solution that once the programmer has imported from QYZlib.threaders that module will used as the threading
module.
On Tue, 27 Dec 2022 at 23:28, Antoon Pardon<antoon.pardon@vub.be> wrote:
At the moment I am happy with a solution that once the programmer hasOh! If that's all you need, then yes, a simple patch of sys.modules
imported from QYZlib.threaders that module will used as the threading
module.
will work. You'll have to make sure you get your module imported
before anything else pulls up the vanilla one, though. Otherwise, your
only option is to keep the existing module object and monkeypatch it
with whatever changes you need.
But a simple "sys.modules['threading'] = QYZlib.threaders" will work.
Of course, how *well* this works depends also on how well that module
manages to masquerade as the threading module, but I'm sure you've
figured that part out :)
Op 27/12/2022 om 11:37 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 21:29, Antoon Pardon <antoon.pardon@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 alternativeEasy: make sure your module is called "threading.py" and is earlier in
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.
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
...
But a simple "sys.modules['threading'] = QYZlib.threaders" will work.
Of course, how *well* this works depends also on how well that module
manages to masquerade as the threading module, but I'm sure you've
figured that part out :)
Well it is what will work for the moment. Thanks for the confirmation
this will indeed work.
Op 27/12/2022 om 13:46 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 23:28, Antoon Pardon<antoon.pardon@vub.be> wrote: >>> At the moment I am happy with a solution that once the programmer has
imported from QYZlib.threaders that module will used as the threadingOh! If that's all you need, then yes, a simple patch of sys.modules
module.
will work. You'll have to make sure you get your module imported
before anything else pulls up the vanilla one, though. Otherwise, your
only option is to keep the existing module object and monkeypatch it
with whatever changes you need.
But a simple "sys.modules['threading'] = QYZlib.threaders" will work.
Of course, how *well* this works depends also on how well that module
manages to masquerade as the threading module, but I'm sure you've
figured that part out :)
Well it is what will work for the moment. Thanks for the confirmation
this will indeed work.
On 12/27/2022 8:25 AM, Antoon Pardon wrote:
Op 27/12/2022 om 13:46 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 23:28, Antoon Pardon<antoon.pardon@vub.be>
wrote:
At the moment I am happy with a solution that once the programmer hasOh! If that's all you need, then yes, a simple patch of sys.modules
imported from QYZlib.threaders that module will used as the threading
module.
will work. You'll have to make sure you get your module imported
before anything else pulls up the vanilla one, though. Otherwise, your
only option is to keep the existing module object and monkeypatch it
with whatever changes you need.
But a simple "sys.modules['threading'] = QYZlib.threaders" will work.
Of course, how *well* this works depends also on how well that module
manages to masquerade as the threading module, but I'm sure you've
figured that part out :)
Well it is what will work for the moment. Thanks for the confirmation
this will indeed work.
What you **should not** do is to make other modules use your code when
they think they are using the standard library. Raymond Chen in The
Old New Thing blog often writes about users who want to do a basically similar thing. He always asks "What if some other program wants to do
the same thing?" One case was when a developer wanted his program's
icon to force itself to be at the top position in the Windows task
bar. "What if another program tried this too?" The two would keep
trying to displace each other at the top position.
If you set the PYTHONPATH environmental variable, you can put any
directory you like to be at the start of the module search path after
the current directory. Perhaps this would be enough for your use case?
The problem is that the logging module uses threading, but doesn't allow
you to specify which threading module to use.
Op 27/12/2022 om 16:49 schreef Thomas Passin:
On 12/27/2022 8:25 AM, Antoon Pardon wrote:
Op 27/12/2022 om 13:46 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 23:28, Antoon Pardon<antoon.pardon@vub.be>
wrote:
At the moment I am happy with a solution that once the programmer has >>>>> imported from QYZlib.threaders that module will used as the threading >>>>> module.Oh! If that's all you need, then yes, a simple patch of sys.modules
will work. You'll have to make sure you get your module imported
before anything else pulls up the vanilla one, though. Otherwise, your >>>> only option is to keep the existing module object and monkeypatch it
with whatever changes you need.
But a simple "sys.modules['threading'] = QYZlib.threaders" will work.
Of course, how *well* this works depends also on how well that module
manages to masquerade as the threading module, but I'm sure you've
figured that part out :)
Well it is what will work for the moment. Thanks for the confirmation
this will indeed work.
What you **should not** do is to make other modules use your code when
they think they are using the standard library. Raymond Chen in The
Old New Thing blog often writes about users who want to do a basically
similar thing. He always asks "What if some other program wants to do
the same thing?" One case was when a developer wanted his program's
icon to force itself to be at the top position in the Windows task
bar. "What if another program tried this too?" The two would keep
trying to displace each other at the top position.
If you set the PYTHONPATH environmental variable, you can put any
directory you like to be at the start of the module search path after
the current directory. Perhaps this would be enough for your use case?
But if I put a threading.py file in that directory, it would make other modules use my code when they "think" they are using the standard library.
The problem is that the logging module uses threading, but doesn't allow
you to specify which threading module to use. So if you want to use an alternative, you either have to do things like this, or copy the logging packages into your project and adapt it to use your alternative.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 78:38:01 |
Calls: | 6,716 |
Files: | 12,247 |
Messages: | 5,357,831 |