obtaining pid of child process

Discussion in 'Python' started by tazimk, Jul 26, 2010.

  1. tazimk

    tazimk Guest

    Hi,

    I am using python's multiprocessing module to spawn new process

    as follows :

    import multiprocessing
    import os
    d = multiprocessing.Process(target=os.system,args=('iostat 2 >
    a.txt',))
    d.start()

    I want to obtain pid of iostat command or the command executed using
    multiprocessing module

    When I execute :

    d.pid

    it gives me pid of subshell in which this command is running .

    Any help will be valuable .

    Thanks in advance
     
    tazimk, Jul 26, 2010
    #1
    1. Advertising

  2. tazimk

    Chris Rebert Guest

    On Sun, Jul 25, 2010 at 9:02 PM, tazimk <> wrote:
    > Hi,
    >
    > I am using python's multiprocessing module to spawn new process
    >
    > as follows :
    >
    > import multiprocessing
    > import os
    > d = multiprocessing.Process(target=os.system,args=('iostat 2 >
    > a.txt',))
    > d.start()
    >
    > I want to obtain pid of iostat command or the command executed using
    > multiprocessing module


    `multiprocessing` isn't the best module for this; use `subprocess` instead:

    from subprocess import Popen, PIPE
    process = Popen(["iostat"], stderr=open("a.txt", 'w'), stdout=PIPE)
    print("the PID is", process.pid)

    `multiprocessing` is used for parallelism in Python code, as an
    alternative to threads. `subprocess` is used for running external
    commands, as a preferred alternative to os.system() among other
    things.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Jul 26, 2010
    #2
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,157
    David Schwartz
    Dec 9, 2003
  2. klappnase

    Capture PID of child process

    klappnase, Aug 5, 2003, in forum: Python
    Replies:
    4
    Views:
    488
  3. P.S.
    Replies:
    0
    Views:
    329
  4. Miki Tebeka
    Replies:
    2
    Views:
    618
    Chris Angelico
    Apr 8, 2011
  5. Z W
    Replies:
    0
    Views:
    182
Loading...

Share This Page