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

发布于 2024-07-22 15:02:05 字数 391 浏览 13 评论 0原文

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

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

我尝试过使用 pexpectos.execlpos.forkpty 但它们都没有达到我的预期。

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

是否有任何图书馆/最佳实践/等。 完成这项工作?

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

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

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 some data, write the output to the terminal which I could read with pexpect and then exit).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萧瑟寒风 2024-07-29 15:02:05

请参阅子流程模块

子进程模块允许您生成新进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码。 该模块旨在替换其他几个较旧的模块和功能,例如:

操作系统

os.spawn*

os.popen*

popen2.*

命令。*

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

青萝楚歌 2024-07-29 15:02:05

您想专用一个终端一个 python shell?

您已经对 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)

根据需要创建任意数量的子级/shell,然后使用 child.before() 或 child.after() 来获取响应。

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

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

注意:我还没有测试上面的代码。 我只是根据过去的经验来回答 pexpect。

You want to dedicate one terminal or one 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.

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

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

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

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

森末i 2024-07-29 15:02:05

我不明白为什么你需要期待这个。 tshark 应该将其输出发送到 stdout,并且只有出于某些奇怪的原因才会将其发送到 stderr。

因此,你想要的应该是:

import subprocess

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

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
帅气尐潴 2024-07-29 15:02:05

从 Python 3.5 开始,您可以执行以下操作:

    import subprocess

    result = subprocess.run(['python', 'my_script.py', '--arg1', val1])
    if result.returncode != 0:
        print('script returned error')

这也会自动重定向 stdout 和 stderr。

From Python 3.5 onwards you can do:

    import subprocess

    result = subprocess.run(['python', 'my_script.py', '--arg1', val1])
    if result.returncode != 0:
        print('script returned error')

This also automatically redirects stdout and stderr.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文