Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   popening a process in a specific working directory (http://www.velocityreviews.com/forums/t596143-popening-a-process-in-a-specific-working-directory.html)

Michael Torrie 03-05-2008 05:01 AM

popening a process in a specific working directory
 
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

Dennis Lee Bieber 03-05-2008 07:12 AM

Re: popening a process in a specific working directory
 
On Tue, 04 Mar 2008 22:01:57 -0700, Michael Torrie <torriem@gmail.com>
declaimed the following in comp.lang.python:

> 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
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Sion Arrowsmith 03-05-2008 12:23 PM

Re: popening a process in a specific working directory
 
Michael Torrie <torriem@gmail.com> wrote:
>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.

--
\S -- siona@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump


All times are GMT. The time now is 02:26 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57