如何从python程序中生成多个python脚本?

时间:2022-03-18 20:25:52

I want to spawn (fork?) multiple Python scripts from my program (written in Python as well).

我想从我的程序中生成(fork?)多个Python脚本(也用Python编写)。

My problem is that I want to dedicate one terminal to each script, because I'll gather their output using pexpect.

我的问题是我想为每个脚本专用一个终端,因为我将使用pexpect收集它们的输出。

I've tried using pexpect, os.execlp, and os.forkpty but neither of them do as I expect.

我已经尝试过使用pexpect,os.execlp和os.forkpty,但它们都没有像我预期的那样。

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

我想生成子进程并忘记它们(它们将处理一些数据,将输出写入终端,我可以用pexpect读取然后退出)。

Is there any library/best practice/etc. to accomplish this job?

有没有图书馆/最佳实践/等。完成这份工作?

p.s. 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.

附:在你问我为什么要写STDOUT并从中读取之前,我会说我不写STDOUT,我读了tshark的输出。

3 个解决方案

#1


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.*

#2


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.

我不明白为什么你需要这个。 tshark应该将其输出发送到stdout,并且只是出于某种奇怪的原因它会将它发送给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

#3


You want to dedicate one terminal or one python shell?

你想专用一个终端或一个python shell?

You already have some useful answers for Popen and Subprocess, you could also use pexpect if you're already planning on using it anyways.

你已经为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)

Make as many children/shells as you want and then use the child.before() or child.after() to get your responses.

根据需要创建多个子/ shell,然后使用child.before()或child.after()来获取响应。

Of course you would want to add definitions or classes to be sent instead of "mycommand1", but this is just a simple example.

当然,您可能希望添加要发送的定义或类而不是“mycommand1”,但这只是一个简单的示例。

If you wanted to make a bunch of terminals in linux, you just need to replace the 'python' in the pextpext.spawn line

如果你想在linux中制作一堆终端,你只需要替换pextpext.spawn行中的'python'

Note: I haven't tested the above code. I'm just replying from past experience with pexpect.

注意:我没有测试过上面的代码。我只是回答过去与pexpect的经历。

#1


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.*

#2


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.

我不明白为什么你需要这个。 tshark应该将其输出发送到stdout,并且只是出于某种奇怪的原因它会将它发送给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

#3


You want to dedicate one terminal or one python shell?

你想专用一个终端或一个python shell?

You already have some useful answers for Popen and Subprocess, you could also use pexpect if you're already planning on using it anyways.

你已经为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)

Make as many children/shells as you want and then use the child.before() or child.after() to get your responses.

根据需要创建多个子/ shell,然后使用child.before()或child.after()来获取响应。

Of course you would want to add definitions or classes to be sent instead of "mycommand1", but this is just a simple example.

当然,您可能希望添加要发送的定义或类而不是“mycommand1”,但这只是一个简单的示例。

If you wanted to make a bunch of terminals in linux, you just need to replace the 'python' in the pextpext.spawn line

如果你想在linux中制作一堆终端,你只需要替换pextpext.spawn行中的'python'

Note: I haven't tested the above code. I'm just replying from past experience with pexpect.

注意:我没有测试过上面的代码。我只是回答过去与pexpect的经历。