popening a process in a specific working directory

Discussion in 'Python' started by Michael Torrie, Mar 5, 2008.

  1. 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
     
    Michael Torrie, Mar 5, 2008
    #1
    1. Advertising

  2. On Tue, 04 Mar 2008 22:01:57 -0700, Michael Torrie <>
    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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Mar 5, 2008
    #2
    1. Advertising

  3. Michael Torrie <> 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 -- -- 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
     
    Sion Arrowsmith, Mar 5, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?ZGF2aWQ=?=
    Replies:
    4
    Views:
    515
    Lucas Tam
    Aug 25, 2005
  2. Priyanka AGARWAL
    Replies:
    9
    Views:
    10,031
    Gordon Beaton
    May 25, 2004
  3. =?Utf-8?B?SmF2?=

    Is ViwState Page-Specific or UserControl-Specific

    =?Utf-8?B?SmF2?=, Aug 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    557
    =?Utf-8?B?SmF2?=
    Aug 16, 2006
  4. mazdotnet
    Replies:
    2
    Views:
    418
    Alexey Smirnov
    Oct 2, 2009
  5. William FERRERES
    Replies:
    7
    Views:
    226
    William FERRERES
    Jul 9, 2007
Loading...

Share This Page