Threading the Python interpreter

Nick Stinemates nick at stinemates.org
Mon Feb 18 21:09:40 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?
>   
FYI -- That's not multi threading that's multiprocessing. You're safe.

-- 
==================
Nick Stinemates (nick at stinemates.org)
http://nick.stinemates.org

AIM: Nick Stinemates
MSN: nickstinemates at hotmail.com
Yahoo: nickstinemates at yahoo.com
==================





More information about the Python-list mailing list