Python and Unix Commands

Discussion in 'Python' started by timdoyle05, Sep 19, 2005.

  1. timdoyle05

    timdoyle05 Guest

    Hi,

    I have a question relating to how Unix commands can be issued from
    Python programs. Im am currently writing a large test script in python
    and I need this script to call three other separate Python scripts,
    where each one executes in it own thread of control. I would like to
    use a Unix command to get each script to run in its own shell. I have
    tried using the "Commands" module but after I issue the
    first "command", execution blocks until I kill the called script. Is
    there a way around this??

    Thanks for your time,
    I would really appreciate any assistance,

    Tim.
     
    timdoyle05, Sep 19, 2005
    #1
    1. Advertising

  2. timdoyle05

    Alan Gauld Guest

    > I have a question relating to how Unix commands can be issued
    > from
    > Python programs.


    There are numerous methods including the commands module that
    you have already discovered, the os.system() call and the
    various os.popen/os.execXX calls.

    Also Python 2.4 has introduced the subprocess module that
    superceded most of these.

    > and I need this script to call three other separate Python
    > scripts,
    > where each one executes in it own thread of control.


    Do you actually need to execute the python scripts as scripts,
    each with its own instance of the intrerpreter? (For testing
    the answer may well be yes) Or would it be possible to simply
    execute the script code as a function from within a thread of
    your main program?

    commands/os.system/subprocess etc are usually used to launch
    Unix commands or other executable files, not other Python
    scripts. It's easier to interact with Python from within
    Python...

    > use a Unix command to get each script to run in its own shell.
    > I have
    > tried using the "Commands" module but after I issue the
    > first "command", execution blocks until I kill the called
    > script. Is
    > there a way around this??


    If you must do this then I'd suggest you need to launch a
    separate
    thread per shell then use commands/subprocess to launch each
    interpreter/script invocation from within its own thread.

    [ If you are not interested in the results you could just use
    the Unix '&' character to execute the processes in the
    background ]

    HTH,


    --
    Alan G
    Author of the Learn to Program web tutor
    http://www.freenetpages.co.uk/hp/alan.gauld
     
    Alan Gauld, Sep 19, 2005
    #2
    1. Advertising

  3. >>>>> "timdoyle05" <> (T) wrote:

    >T> Hi,
    >T> I have a question relating to how Unix commands can be issued from
    >T> Python programs. Im am currently writing a large test script in python
    >T> and I need this script to call three other separate Python scripts,
    >T> where each one executes in it own thread of control. I would like to
    >T> use a Unix command to get each script to run in its own shell. I have
    >T> tried using the "Commands" module but after I issue the
    >T> first "command", execution blocks until I kill the called script. Is
    >T> there a way around this??


    Use the subprocess module. If you have an older python, you could use
    os.system(command + "&") or install the subprocess module yourself.
    --
    Piet van Oostrum <>
    URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Sep 19, 2005
    #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. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,982
    Thomas Stegen
    Jun 28, 2003
  2. Kory Wheatley

    Unix commands within Python code

    Kory Wheatley, Jul 15, 2004, in forum: Python
    Replies:
    3
    Views:
    11,992
    Jeremy Sanders
    Jul 16, 2004
  3. Tim Stanka
    Replies:
    1
    Views:
    812
    Jeff Epler
    Aug 2, 2004
  4. Rob Cowie
    Replies:
    5
    Views:
    436
    Will McDonald
    Dec 17, 2005
  5. AS

    Unix commands and perl

    AS, Nov 28, 2004, in forum: Perl Misc
    Replies:
    13
    Views:
    194
    Ben Morrow
    Nov 30, 2004
Loading...

Share This Page