Threading the Python interpreter

Steve Holden steve at holdenweb.com
Mon Feb 18 20:49:54 EST 2008


MooJoo wrote:
> I've read that the Python interpreter is not thread-safe but are there 
> any issues in creating threads that create new processes (not threads) 
> that run new instantiations of python? What I'm doing is subclassing the 
> threading.Thread and, in the run method, I'm making a call to os.system, 
> passing to it another python script which then processes a file. When 
> the call to os.system completes, the thread is finished. Here is a 
> simplified fragment of code for what I'm doing.
> 
> from threading import Thread
> import os
> 
> class MyThread(Thread):
>    def __init__(self, fn):
>       Thread.__init__(self)
>       self.fn = fn
> 
>    def run(self):
>       pyscript = '/usr/bin/env python script.py %s'%self.fn
>       status = os.system(pyscript)
> 
> thr = MyThread('test.dat')
> thr.start()
> thr.join()
> 
> Since I'm running each python instance in a new process, I don't believe 
> that there is a problem and, in my testing so far, I haven't encountered 
> anything that would lead me to believe there is a potential time bomb 
> here. Am I correct in my assumption this is OK or just lucky so far?

You're fine. The interpreters running in separate processes share only 
the pure code (that is the compiled interpreter itself). The Python 
namespaces of the two processes are entirely separate from each other.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list