__builtins__ thread-safe / __builtins__ as function?

Chris Angelico rosuav at gmail.com
Sun Oct 14 10:01:21 EDT 2012


On Sun, Oct 14, 2012 at 9:36 PM, Juergen Bartholomae
<bartholomae.juergen at googlemail.com> wrote:
> Unfortunately, replacing __builtins__ at import time won't do, because
> external modules (that is, .py) get imported only once when they are
> accessed by the first thread, which includes (of course) setting up of
> __dict__ and __builtins__. When a second thread later accesses this
> module, it has the same variables in __builtins__ that were added by
> the same module in first thread

Sure. But if they're using __builtins__ by name, then you can simply
replace that with something that checks a thread id and responds with
that thread's dictionary. There's no particular reason for
__builtins__ to be a module (as far as I know!), so you should be able
to replace it with an object of your own class.

I think DaveA's recommendation is good. Make a "Version 2.0" of your
system, with a different way of doing global state.

ChrisA



More information about the Python-list mailing list