在 Python 子进程下分叉多个应用程序

发布于 2024-08-31 06:39:58 字数 273 浏览 1 评论 0原文

我一直在通过谷歌和SO寻找可以帮助我解决这个问题的东西,但我遇到了障碍。我对 Python 有点陌生,但我正在寻找一种运行多个在后台持续运行的应用程序的方法。

例如,我需要 4 个应用程序来启动,并将参数 -appnum 设置为不同的值。我想使用 python 来计数,然后启动一个将继续运行的新应用程序。

我以为我会使用子流程,但我对文档感到有点不知所措。

我还计划让应用程序打印出数字序列,并希望将其重定向到一个文件。我注意到一些 SO 问题谈到了这一点,但我对该怎么做有点困惑。

I have been looking through Google and SO for something that could help me solve this but I have run into a block. I am a bit new to Python but I am looking for a way to run multiple apps that will continuously run in the background.

For example, I need 4 apps to start up with a param -appnum set to a different value. I would like to use python to count up and then start up a new app that will continue to run.

I assumed I would use subprocess but I feel a bit overwhelmed by the documentation.

I also plan to have the app print out sequences of numbers and would like to redirect this to a file. I noticed some of the SO questions talked about this, but I am a little confused on what to do.

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

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

发布评论

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

评论(1

蓝色星空 2024-09-07 06:39:59

一个简单的开始方法可能是使用 os.popen()< /code>,就像这样:

import os

subprogs = [None] * 4
for i in range(4):
    subprogs[i] = os.popen("app -appnum " + i, "r")

从这里,您可以像读取文件一样从每个 subprog[i] 中读取内容,捕获 app 程序的输出。

请注意,尽管文档说此函数已被弃用,但它对于许多用途仍然可以完美地工作。当您更了解时,您可以探索 subprocess 模块熟悉 os.popen() 的限制。

A simple way to start might be to use os.popen(), like this:

import os

subprogs = [None] * 4
for i in range(4):
    subprogs[i] = os.popen("app -appnum " + i, "r")

From here, you can read from each subprog[i] just like a file, capturing the output of the app program.

Note that although the documentation says this function has been deprecated, it still works perfectly fine for many purposes. You can explore the subprocess module when you're more familiar with the limitations of os.popen().

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