How to create multiple python scripts from a python program?

I want to cut (fork?) Several Python scripts from my program (also written in Python).

My problem is that I want to allocate one terminal for each script, because I will collect their output using pexpect.

I tried to use pexpect, os.execlpand os.forkpty, but none of them work as I expect.

I want to spawn child processes and forget about them (they will process some data, write output to the terminal, which I could read using pexpect, and then exit).

Is there a library / best practice / etc. do this job?

ps Before you ask why I will write to and read from STDOUT, I will say that I do not write to STDOUT, I read the output tshark.

+3
source share
3 answers

See module

+5
source

I donโ€™t understand why you need it. tsharkshould send its output to stdout, and only for some strange reason will it send it to stderr.

Therefore, you must be:

import subprocess

fp= subprocess.Popen( ("/usr/bin/tshark", "option1", "option2"), stdout=subprocess.PIPE).stdout
# now, whenever you are ready, read stuff from fp
0
source

python?

Popen Subprocess, pexpect, .

#for multiple python shells
import pexpect

#make your commands however you want them, this is just one method
mycommand1 = "print 'hello first python shell'"
mycommand2 = "print 'this is my second shell'"

#add a "for" statement if you want
child1 = pexpect.spawn('python')
child1.sendline(mycommand1)

child2 = pexpect.spawn('python')
child2.sendline(mycommand2)

/, , child.before() child.after(), .

, "mycommand1", .

Linux, "python" pextpext.spawn

Note. I have not tested the above code. I just answer from past experience with pexpect.

0
source

Source: https://habr.com/ru/post/1709943/


All Articles