473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

process wrapper?

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?
Jul 18 '05 #1
10 2132
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
Jul 18 '05 #2
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>:
...
| 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

Jul 18 '05 #3
In article <QZ********************@speakeasy.net>,
"Steve @ Waypath" <st***@waypath.com> wrote:
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?


It's a zombie! UNIX leaves a dead process in the table as
long as its parent might come back and ask for the status.
It goes either when the parent gets its status with a wait
function (like waitpid()), or when the parent exits.

Donn Cave, do**@drizzle.com
Jul 18 '05 #4
"Donn Cave" <do**@u.washington.edu> wrote in message
news:do************************@nntp4.u.washington .edu...
In article <QZ********************@speakeasy.net>,
"Steve @ Waypath" <st***@waypath.com> wrote:
##########
File: test1.py
------------
import os, time
if __name__=='__main__':
os.spawnv(os.P_NOWAIT,'python',['test2.py'])

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.


This makes sense to me. I tried this originally, and tried again just now.
For reasons not clear to me, I get the error:
bad interpreter: No such file or directory
when I include the path to the python interperter. I've tried a number of
approaches:
#!python
#!/usr/bin/python
#!/usr/bin/env python

(I've got the x bit set on the test files, so it's not a permissions
problem. ) This, I'm guessing, isn't a python problem, and would seem to be
my roadblock. I've done some preliminary searches for an answer, but nothing
has panned out so far; if anyone has any ideas...
###########
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?
It's a zombie! UNIX leaves a dead process in the table as
long as its parent might come back and ask for the status.
It goes either when the parent gets its status with a wait
function (like waitpid()), or when the parent exits.


I've read other posts (many yours, I think :) indicating something like
this, but I confess I don't understand the relationship between waitpid()
and spawnv(), how I would use the former with the latter. However, as I
recognize that isn't the solution to my problem, I won't ask you to explain
it--unless you really want to. :)

Donn Cave, do**@drizzle.com

Jul 18 '05 #5
In article <2p********************@speakeasy.net>,
"Steve @ Waypath" <st***@waypath.com> wrote:
....
For reasons not clear to me, I get the error:
bad interpreter: No such file or directory
when I include the path to the python interperter. I've tried a number of
approaches:
#!python
#!/usr/bin/python
#!/usr/bin/env python

(I've got the x bit set on the test files, so it's not a permissions
problem. ) This, I'm guessing, isn't a python problem, and would seem to be
my roadblock. I've done some preliminary searches for an answer, but nothing
has panned out so far; if anyone has any ideas...
The first one wouldn't work for sure. Second one works if python
is in /usr/bin, third one works if python is in some directory in
PATH and env is in /usr/bin. Assuming your own shell is bash,
try "type python" and "type env" to see where those programs are.

[re zombie processes] I've read other posts (many yours, I think :) indicating something like
this, but I confess I don't understand the relationship between waitpid()
and spawnv(), how I would use the former with the latter.


spawnv returns a process ID, if you use os.P_NOWAIT. It returns
the process exit status if you use os.P_WAIT. If you look at the
code, in os.py, you will see that this second variation is essentially
implemented by applying waitpid() to the first.

Donn Cave, do**@drizzle.com
Jul 18 '05 #6

"Donn Cave" <do**@u.washington.edu> wrote in message
news:do************************@nntp6.u.washington .edu...
The first one wouldn't work for sure. Second one works if python
is in /usr/bin, third one works if python is in some directory in
PATH and env is in /usr/bin. Assuming your own shell is bash,
try "type python" and "type env" to see where those programs are.
I think I found the problem here. I was editing in Windows, saving to a
mounted Linux drive, running in Linux. It looks like some end-of-line chars
I didn't account for. I'm still having defunct trouble, but I'm going to
split this one off into a separate thread, going back a few steps.

spawnv returns a process ID, if you use os.P_NOWAIT. It returns
the process exit status if you use os.P_WAIT. If you look at the
code, in os.py, you will see that this second variation is essentially
implemented by applying waitpid() to the first.
So, if I'm following this correctly, if I use os.P_NOWAIT, I shouldn't have
to apply waitpid(), no? And, then, I shouldn't have a zombie process if I
use P_NOWAIT, either. Am I getting this?

Donn Cave, do**@drizzle.com

Jul 18 '05 #7
> I think I found the problem here. I was editing in Windows, saving to a
mounted Linux drive, running in Linux. It looks like some end-of-line chars I didn't account for. I'm still having defunct trouble, but I'm going to
split this one off into a separate thread, going back a few steps.


Donn,

Forget that separate thread. Writing in Linux, instead of Windows, and all
my spawnv problems go away. I've got enough progress here to keep me busy
for a while. I appreciate your help.

If I run into trouble again, you'll hear from me. :)

Best,
Steve
Jul 18 '05 #8
In article <5q********************@speakeasy.net>,
"Steve @ Waypath" <st***@waypath.com> wrote:
"Donn Cave" <do**@u.washington.edu> wrote in message
news:do************************@nntp6.u.washington .edu...

spawnv returns a process ID, if you use os.P_NOWAIT. It returns
the process exit status if you use os.P_WAIT. If you look at the
code, in os.py, you will see that this second variation is essentially
implemented by applying waitpid() to the first.


So, if I'm following this correctly, if I use os.P_NOWAIT, I shouldn't have
to apply waitpid(), no? And, then, I shouldn't have a zombie process if I
use P_NOWAIT, either. Am I getting this?


Well, no, I believe you have it backwards.

Donn Cave, do**@drizzle.com
Jul 18 '05 #9
There should be a waiting period on posting. Yes, of course it's the other
way.

In practice, when does one use P_NOWAIT and then waitpid(), instead of
P_WAIT? I'm guessing the answer is "if you want to do something between the
spawing and the waiting," but is there a standard use I haven't learned yet?

I'm guessing your reponse will finish this thread. So, thanks again for all
the direction.

"Donn Cave" <do**@u.washington.edu> wrote in message
news:do************************@nntp4.u.washington .edu...
In article <5q********************@speakeasy.net>,
"Steve @ Waypath" <st***@waypath.com> wrote:
"Donn Cave" <do**@u.washington.edu> wrote in message
news:do************************@nntp6.u.washington .edu...

spawnv returns a process ID, if you use os.P_NOWAIT. It returns
the process exit status if you use os.P_WAIT. If you look at the
code, in os.py, you will see that this second variation is essentially
implemented by applying waitpid() to the first.


So, if I'm following this correctly, if I use os.P_NOWAIT, I shouldn't have to apply waitpid(), no? And, then, I shouldn't have a zombie process if I use P_NOWAIT, either. Am I getting this?


Well, no, I believe you have it backwards.

Donn Cave, do**@drizzle.com

Jul 18 '05 #10
In article <Ix********************@speakeasy.net>,
"Steve @ Waypath" <st***@waypath.com> wrote:
....
In practice, when does one use P_NOWAIT and then waitpid(), instead of
P_WAIT? I'm guessing the answer is "if you want to do something between the
spawing and the waiting," but is there a standard use I haven't learned yet?


Yes, that's the idea.

Donn Cave, do**@drizzle.com
Jul 18 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Berata | last post by:
Hello all, in VB6 we were able to create ActiveX-DLL's (In Process Components) and ActiveX-Exe's (Out of Process Components). We habe build up an application that exists of an main module...
1
by: JC | last post by:
I'm trying to create a GUI wrapper for dumpbin, and so I'm using the Process class to run the command-line application. The problem is that if I use Readline() to get the output from the...
1
by: Ram | last post by:
Hey, I'm using the Log4Net opensource object to write to my logs. I'v written a wrapper ClassLibrary project for the Log4Net and all my projects reference only to it. I'v got a web application...
3
by: rh0dium | last post by:
Hi all, Here I was happily coming to working thinking - OK I need to create a wrapper for a tool (UNIX) which does nothing but lauch the end tool and send a sql instert letting the db know the...
0
by: IndulgentCoder | last post by:
Hi all, I'm new to scripts forum. I've a .NET wrapper class written in c# that makes some calls to native dlls using platform invocation. The native dlls inturn perform read operation on MSOffice...
7
by: Gamma | last post by:
I'm trying to create a ListBox that holds list of currently running processes. So I inherited from Process a classes named _Process, which it's objects are to be held in the ListBox (I just...
2
by: Erick | last post by:
I've create a windows service which will start any windows application whose name it finds in an xml file i have pointed it to. However, I also want it to create .net objects from assemblies...
7
by: atlaste | last post by:
Hi, I have two different things I'd like to discuss here. Both are about cross-process synchronization of shared resources. The resources that are shared are: (1) an object writing to a file and...
0
by: Miles | last post by:
On Mon, Aug 18, 2008 at 9:34 AM, srinivasan srinivas wrote: If ps + grep (or the more robust tool pgrep) won't work because you're running more than one instance at once, try this: instead of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.