在 Python 子进程下分叉多个应用程序
我一直在通过谷歌和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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个简单的开始方法可能是使用
os.popen()< /code>
,就像这样:
从这里,您可以像读取文件一样从每个
subprog[i]
中读取内容,捕获app
程序的输出。请注意,尽管文档说此函数已被弃用,但它对于许多用途仍然可以完美地工作。当您更了解时,您可以探索
subprocess
模块熟悉 os.popen() 的限制。A simple way to start might be to use
os.popen()
, like this:From here, you can read from each
subprog[i]
just like a file, capturing the output of theapp
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 ofos.popen()
.