obtaining pid of child process

Chris Rebert clp2 at rebertia.com
Mon Jul 26 00:29:09 EDT 2010


On Sun, Jul 25, 2010 at 9:02 PM, tazimk <tazimkolhar at gmail.com> 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



More information about the Python-list mailing list