流程包装? [英] process wrapper?

查看:37
本文介绍了流程包装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助。我正在尝试用Python写一个进程包装器类(在
Linux上),让它成为一个:

- 从配置文件中读取服务定义(其中)服务定义

包含一个启动服务的bash命令,该服务是一个守护进程)

- 调用一个启动服务的方法

- 调用一种关闭服务的方法。

- 其他与此无关的东西


我难倒的地方是在启动该服务的方式如下:

- 不会阻止主进程的运行(即将控制权返还给包装器的
,因此它会监听更多命令)

- 让我在启动的服务上得到一些处理,我可以稍后使用b $ b来关闭服务。


我已经尝试了os.system,os.popen *,os.spawn *和

(os.fork + os.exec *)的每个实现可以找到一个例子,或者我可以想象,

但我不能想出任何对我有用的东西。 os.spawnv()看起来很有希望,但是我开始的每一个过程都已经不复存在了。


我不会厌倦你的东西的例子那不行。我已经尝试了很多东西,很多东西,我的头在旋转。我不在乎我是否将进程包装在我的主进程控制/杀死的

处理程序中,或者如果我分离进程但是

跟踪它的进程PID,以后再杀了。我不在乎PID是在内存中还是写在我以后可以阅读的文件中的



任何人都可以指出我正确的方向?

I need help. I''m trying to write a process wrapper class in Python (on
Linux) that let''s one:
- read service definitions from a config file (where a service definition
includes a bash command to start the service, and the service is a daemon)
- call a method that will start up the service
- call a method that will shut down the service.
- other stuff not relevant here

Where I''m stumped is in starting up the service in a way that:
- doesn''t block the running of the main process (i.e. returns control back
to the wrapper, so it will listen for more commands)
- let''s me get some kind of handle on the started service that I can later
use to shut down the service.

I''ve tried every implementation of os.system, os.popen*, os.spawn* , and
(os.fork + os.exec*) for which I can find an example or that I can imagine,
but I can''t come up with anything that works for me. os.spawnv() looked
very promising, but every process I start with it goes defunct.

I won''t bore you with examples of stuff that doesn''t work. I''ve tried so
many things, my head is spinning. I don''t care if I wrap the process in a
handler that my main process controls/kills or if I spin off a process but
track it''s PID, to kill later. I don''t care if the PID is in memory or
written to a file I can read later.

Can anyone point me in the right direction?

推荐答案

Quoth" Steve @ Waypath" < st *** @ waypath.com>:

....

|我已经尝试了os.system的每个实现,os.popen *,os.spawn *和

| (os.fork + os.exec *),我可以找到一个例子,或者我可以想象,

|但我不能想出任何对我有用的东西。 os.spawnv()看了

|非常有希望,但我开始的每一个过程都会失效。


嗯,你需要解决这个问题,不是吗? spawnv适合人们使用,而且很可能它对你的应用来说是完美的。所以从

开始,这个简单的Python程序在一些普通的实用工具上使用spawnv,

就像date一样。例如,如果你不能让它工作,你将会b / b
有一个更具体的问题。


Donn Cave,做** @ drizzle.com
Quoth "Steve @ Waypath" <st***@waypath.com>:
....
| I''ve tried every implementation of os.system, os.popen*, os.spawn* , and
| (os.fork + os.exec*) for which I can find an example or that I can imagine,
| but I can''t come up with anything that works for me. os.spawnv() looked
| very promising, but every process I start with it goes defunct.

Well, you need to fix that, no? spawnv works for people, and very
likely it''s the perfect thing for your application. So start with
a simple Python program that uses spawnv on some ordinary utility,
like "date" for example, and if you can''t get it to work you will
have a more specific question to pose.

Donn Cave, do**@drizzle.com


Donn,


感谢您的回复。我还没有实现一个有效的spawnv调用,所以

我不相信我的测试是有效的。以下是样本:


##########

文件:test1.py

- -----------

导入操作系统,时间

如果__name __ ==''__ main__'':

os .spawnv(os.P_NOWAIT,''python'',[''test2.py''])

for step in range(10):

print'' test1签入''

time.sleep(1)


##########

档案:test2.py

------------

导入时间

if __name __ ==''__ main__' ':

for step in range(10):

print''test2 check in''

time.sleep(1)


##########

stdout:

#python test.py

test1签入

....

test1签入

[10次''test1 checkin in'';没有''test2 check in'']


###########

当测试1正在运行时,ps(在另一个shell中):

#ps x | grep python

11726 pts / 1 S 0:00 python test1.py

11727 pts / 1 Z 0:00 [python< defunct>]

11729 pts / 2 S 0:00 grep python


###########

我看到这个已经不复存在的东西了我尝试的每个spawnv测试。当调用进程(在本例中为test1.py)结束时,已失效的进程

消失。那里

我在这里错了吗?


谢谢

Steve @ Waypath.com


Donn Cave <做** @ drizzle.com>在消息中写道

news:1080539401.173853@yasure ...
Donn,

Thanks for the reply. I haven''t yet implemented a working spawnv call, so
I''m not confident my tests are valid. Here''s a sample:

##########
File: test1.py
------------
import os, time
if __name__==''__main__'':
os.spawnv(os.P_NOWAIT,''python'',[''test2.py''])
for step in range(10):
print ''test1 checking in''
time.sleep(1)

##########
File: test2.py
------------
import time
if __name__==''__main__'':
for step in range(10):
print ''test2 checking in''
time.sleep(1)

##########
stdout:
# python test.py
test1 checking in
....
test1 checking in
[ 10 iterations of ''test1 checkin in''; none of ''test2 checking in'']

###########
While test 1 is running, a ps (in another shell):
# ps x | grep python
11726 pts/1 S 0:00 python test1.py
11727 pts/1 Z 0:00 [python <defunct>]
11729 pts/2 S 0:00 grep python

###########
I see this defunct thing with every spawnv test I try. The defunct process
goes away when the calling process (test1.py, in this case) finishes. Where
am I going wrong here?

Thanks
Steve @ Waypath.com

"Donn Cave" <do**@drizzle.com> wrote in message
news:1080539401.173853@yasure...
Quoth" Steve @ Waypath" < st *** @ waypath.com>:
...
|我已经尝试过os.system,os.popen *,os.spawn *和
|的每个实现(os.fork + os.exec *),我可以找到一个例子,或者我可以想象,
但我不能想出任何对我有用的东西。 os.spawnv()看了
|非常有希望,但我开始的每一个过程都会失效。

嗯,你需要解决这个问题,不是吗? spawnv适合人们使用,而且很可能它对您的应用程序来说是完美的。所以从一个简单的Python程序开始,该程序在一些普通的实用工具上使用spawnv,
就像date一样。例如,如果你不能让它工作,你将会有一个更具体的问题。

Donn Cave,做** @ drizzle.com
Quoth "Steve @ Waypath" <st***@waypath.com>:
...
| I''ve tried every implementation of os.system, os.popen*, os.spawn* , and
| (os.fork + os.exec*) for which I can find an example or that I can imagine, | but I can''t come up with anything that works for me. os.spawnv() looked
| very promising, but every process I start with it goes defunct.

Well, you need to fix that, no? spawnv works for people, and very
likely it''s the perfect thing for your application. So start with
a simple Python program that uses spawnv on some ordinary utility,
like "date" for example, and if you can''t get it to work you will
have a more specific question to pose.

Donn Cave, do**@drizzle.com



文章< QZ ********** **********@speakeasy.net> ;,

" Steve @ Waypath" < ST *** @ waypath.com>写道:
In article <QZ********************@speakeasy.net>,
"Steve @ Waypath" <st***@waypath.com> wrote:
感谢您的回复。我还没有实现一个有效的spawnv调用,所以
我不相信我的测试是有效的。这是一个样本:

##########
文件:test1.py
------------
import os,time
如果__name __ ==''__ main__'':
os.spawnv(os.P_NOWAIT,''python'',[''test2.py''])


好​​的,可以修复。


当文档说路径时对于某些功能,和

" file"对于其他人(例如,spawnvp),区别在于

" path"包括目录规范,绝对

或相对于当前工作目录。 spawnv想要

一条路径,所以除非python,否则上述方法不起作用是您当前工作目录中的文件

。要解决这个问题,

我会写绝对路径,或者你可以使用spawnvp

代替。


但是不要给它蟒蛇的路径。相反,至少考虑直接调用Python程序文件的

- 把它的路径放在那里

作为第二个参数,而不是python'的。然后您的参数列表

(第3个参数)将正确写入。如果你调用

python本身,那么你需要使test2.py成为第二个

参数,指示python解释该文件。如果你决定按照我的建议做b $ b,那么test2.py将必须是可执行的

(chmod 755)并以#!/ usr /之类的行开头bin / python",

适用于您的系统。从那以后,你的test1.py

程序不再需要知道test2.py实际上是如何实现的。


###########
当测试1正在运行时,ps(在另一个shell中):
#ps x | grep python
11726 pts / 1 S 0:00 python test1.py
11727 pts / 1 Z 0:00 [python< defunct>]
11729 pts / 2 S 0:00 grep python

###########
我在尝试的每个spawnv测试中看到这个已经不存在的东西。当调用进程(在本例中为test1.py)完成时,已失效的进程将消失。我在哪里错了?
Thanks for the reply. I haven''t yet implemented a working spawnv call, so
I''m not confident my tests are valid. Here''s a sample:

##########
File: test1.py
------------
import os, time
if __name__==''__main__'':
os.spawnv(os.P_NOWAIT,''python'',[''test2.py''])
OK, that can be fixed.

When the documentation says "path" for some functions, and
"file" for others (e.g., spawnvp), the distinction is that
"path" includes the directory specification, either absolute
or relative to the current working directory. spawnv wants
a path, so the above doesn''t work unless "python" is a file
in your current working directory. To resolve this problem,
I would just write in the absolute path, or you may use spawnvp
instead.

But don''t give it python''s path. Instead, at least consider
invoking the Python program file directly - put its path there
as the 2nd parameter, not python''s. Then your argument list
(the 3rd parameter) will be correct as written. If you invoke
python itself, then you''ll need to make test2.py the second
argument, directing python to interpret that file. If you
decide to do as I suggest, test2.py will have to be executable
(chmod 755) and start with a line like "#!/usr/bin/python", as
appropriate for your system. From there on, your test1.py
program no longer needs to know how test2.py is actually
implemented.

###########
While test 1 is running, a ps (in another shell):
# ps x | grep python
11726 pts/1 S 0:00 python test1.py
11727 pts/1 Z 0:00 [python <defunct>]
11729 pts/2 S 0:00 grep python

###########
I see this defunct thing with every spawnv test I try. The defunct process
goes away when the calling process (test1.py, in this case) finishes. Where
am I going wrong here?




这是一个僵尸! UNIX在表中留下了一个死进程,因为它的父级可能会回来询问状态。

当父进程等待状态时,它会进入

函数(如waitpid()),或父母退出时。


Donn Cave,做** @ drizzle.com


这篇关于流程包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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