如何判断分叉进程是否已完成? [英] How to tell if a forked process is done?

查看:84
本文介绍了如何判断分叉进程是否已完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我想知道如何判断分叉过程是否完成。


实际上,我真正的问题是我想在一个python脚本的
里面运行一个shell脚本,并且在shell脚本运行完毕后,我想要b / b
想要做更多东西*条件* shell脚本

已经完成运行,在同一个python脚本中。


我能想到的唯一方法是分叉一个进程,然后调用

shell脚本,如:

pid = os.fork()

如果pid == 0:

os.execl(shellscript_name.sh,"")

但我怎么知道shell脚本是否已完成?


总之,我的两个问题是:

1.我如何知道分叉的shell脚本是否已完成?

2.如何在没有
分支一个新进程,以便我可以在同一个python脚本中知道shell脚本是否已完成

? br />

提前谢谢,


John

解决方案

你可以只执行os.system(some command),它将阻塞,直到shell完成

命令,并返回退出代码。如果你需要更多控制,

或许可以查看popen2模块和其中的Popen3 / 4类。


-

Nick Welch又名mackstann | mack @ incise.org | http://incise.org

帮助标记并取消冗余。


文章< 87 ************************** @ posting.postoutgoogle.com>,
jo****@ugcs.caltech.edu (约翰林)写道:

我想知道如何判断分叉过程是否完成。

实际上,我真正的问题是我想要运行一个一个python脚本里面的shell脚本,并且在shell脚本运行完毕之后,我想在shell脚本运行完毕的情况下做更多东西*条件*,里面我想到的唯一方法是分叉一个进程,然后调用
shell脚本,如:
pid = os.fork()
如果pid == 0:
os.execl(shellscript_name.sh,"")
但我怎么知道shell脚本是否已完成?
我总而言之,我的两个问题是:
1.我如何知道分叉的shell脚本是否已完成?
2.如何在python脚本中运行shell脚本而不需要分析新进程,以便我可以从同一个python脚本中知道shell脚本是什么?




最简单的方法来做你想要的东西是
os.system(" shellscript_path")


如果任何命令行实际上要来了

来自输入数据,或者你有其他理由喜欢

像exec这样的参数列表,参见os.spawnv和类似的,

,os.P_WAIT作为第一个参数。或者如果您的下一个问题是如何从两个进程之间的管道中读取,请参阅os.popen()作为初学者。


所有这些函数都会分叉一个进程,但它们使用

waitpid或类似的函数暂停调用

进程,直到fork退出。请参阅man 2 waitpid,其中
可从python获得,如posix.waitpid()或os.waitpid()。


Donn Cave,做** @ u.washington.edu


在某些时候, jo **** @ ugcs.caltech.edu (John Lin)写道:

你好,

我想知道如何判断分叉进程是否完成。

实际上,我真正的问题是我想在内部运行一个shell脚本
一个python脚本,在shell脚本运行完毕后,我希望在shell脚本运行完毕的情况下,在同一个python脚本中做更多的东西*条件*。 />
我能想到的唯一方法是分叉一个进程,然后调用
shell脚本,如:
pid = os.fork()
如果pid = = 0:
os.execl(shellscript_name.sh,"")
但我怎么知道shell脚本是否已完成?


在Python Library Reference中查找os.wait和os.waitpid。或者,对于这种情况,

,使用os.system()。


-

|> | \ / |<

/ -------------------------------------- ------------------------------------ \

| David M.库克

| cookedm(at)physics(dot)mcmaster(dot)ca


Howdy,

I want to know how to tell if a forked process is done.

Actually, my real question is that I want to run a shell script inside
of a python script, and after the shell script has finished running, I
want to do more stuff *condition* on the fact that the shell script
has finished running, inside the same python script.

The only way I can think of is to fork a process and then call the
shell script, as in:
pid = os.fork()
if pid == 0:
os.execl(shellscript_name.sh, "")
but how can I know if the shell script is finished?

In sum, my two questions are:
1. How can I know if a forked shell script is finished?
2. How can I run a shell script inside a python script without
forking a new process, so that I can know the shell script is done
from within the same python script?

thanks in advance,

John

解决方案

You can just do os.system("some command"), it will block until the shell
command is done, and return the exit code. If you need more control,
perhaps look into the popen2 module and the Popen3/4 classes inside it.

--
Nick Welch aka mackstann | mack @ incise.org | http://incise.org
Help stamp out and abolish redundancy.


In article <87**************************@posting.google.com >,
jo****@ugcs.caltech.edu (John Lin) wrote:

I want to know how to tell if a forked process is done.

Actually, my real question is that I want to run a shell script inside
of a python script, and after the shell script has finished running, I
want to do more stuff *condition* on the fact that the shell script
has finished running, inside the same python script.

The only way I can think of is to fork a process and then call the
shell script, as in:
pid = os.fork()
if pid == 0:
os.execl(shellscript_name.sh, "")
but how can I know if the shell script is finished?

In sum, my two questions are:
1. How can I know if a forked shell script is finished?
2. How can I run a shell script inside a python script without
forking a new process, so that I can know the shell script is done
from within the same python script?



The simplest way to do what you appear to want is
os.system("shellscript_path")

If any of the command line is actually going to come
from input data, or you have some other reason to prefer
an argument list like exec, see os.spawnv and similar,
with os.P_WAIT as first parameter. Or if your next
question is going to be how to read from a pipe between
the two processes, see os.popen() for starters.

All of these functions will fork a process, but they use
waitpid or some similar function to suspend the calling
process until the fork exits. See man 2 waitpid, which
is available from python as posix.waitpid() or os.waitpid().

Donn Cave, do**@u.washington.edu


At some point, jo****@ugcs.caltech.edu (John Lin) wrote:

Howdy,

I want to know how to tell if a forked process is done.

Actually, my real question is that I want to run a shell script inside
of a python script, and after the shell script has finished running, I
want to do more stuff *condition* on the fact that the shell script
has finished running, inside the same python script.

The only way I can think of is to fork a process and then call the
shell script, as in:
pid = os.fork()
if pid == 0:
os.execl(shellscript_name.sh, "")
but how can I know if the shell script is finished?



Look up os.wait and os.waitpid in the Python Library Reference. Or,
for this case, use os.system().

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca


这篇关于如何判断分叉进程是否已完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆