popening a process in a specific working directory

M

Michael Torrie

I have a small multi-threaded program that spawns a number of threads
that each spawn a particular process in a particular temporary
directory. My problem is that using os.chdir to change the working
directory before popening the process doesn't always work because
another thread might change the cwd as it starts, before the process in
this thread can start up.

I'm currently using popen2.Popen4. Is there a way to properly specify a
particular working directory when launching a process in python? I've
hacked around my problem by writing a bash wrapper script that accepts
the working directory as a parameter, changes the directory, then spawns
the original program with the arguments. This works, but I'd like a
better way.

Michael
 
D

Dennis Lee Bieber

I have a small multi-threaded program that spawns a number of threads
that each spawn a particular process in a particular temporary
directory. My problem is that using os.chdir to change the working
directory before popening the process doesn't always work because
another thread might change the cwd as it starts, before the process in
this thread can start up.

Sounds like a variation of a classical "critical section"...

Wrap the os.chdir and popen calls with a common lock...

spawnLock.acquire()
os.chgdir(...)
...popen*...
spawnLock.release()

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Sion Arrowsmith

Michael Torrie said:
I'm currently using popen2.Popen4. Is there a way to properly specify a
particular working directory when launching a process in python?

Switch to using subprocess.Popen and specify the cwd argument.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top