obtaining pid of child process

T

tazimk

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
 
C

Chris Rebert

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top