tags:

views:

484

answers:

2

Hi I want to spawn (fork?) multiple Python scripts from my program (written in Python too) My problem is that I want to dedicate one terminal to each script , because I'll gather their output using pexpect. I've tried using pexpect, os.execlp and os.forkpty but neither of them do as I expect. I want to spawn the child processes and forget about them (they will process on some data, write the output to terminal which I could read with pexpect and then exit) Is there any library/best practice/etc to do this job ? (and before you ask why I would write to STDOUT and read from it, I shall say that I don't write to STDOUT, I read the output of tshark)

Thanks

+2  A: 

See the subprocess module

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several other, older modules and functions, such as:

os.system

os.spawn*

os.popen*

popen2.*

commands.*

gimel
A: 

I don't understand why you need expect for this. tshark should send its output to stdout, and only for some strange reason would it send it to stderr.

Therefore, what you want should be:

import subprocess

fp= subprocess.Popen( ("/usr/bin/tshark", "option1", "option2"), stdout=subprocess.PIPE).stdout
# now, whenever you are ready, read stuff from fp
ΤΖΩΤΖΙΟΥ