473,789 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2164
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.co m
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__=='__ma in__':
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__=='__ma in__':
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.c om> 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.co m

Jul 18 '05 #3
In article <QZ************ ********@speake asy.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__=='__ma in__':
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.co m
Jul 18 '05 #4
"Donn Cave" <do**@u.washing ton.edu> wrote in message
news:do******** *************** *@nntp4.u.washi ngton.edu...
In article <QZ************ ********@speake asy.net>,
"Steve @ Waypath" <st***@waypath. com> wrote:
##########
File: test1.py
------------
import os, time
if __name__=='__ma in__':
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.co m

Jul 18 '05 #5
In article <2p************ ********@speake asy.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.co m
Jul 18 '05 #6

"Donn Cave" <do**@u.washing ton.edu> wrote in message
news:do******** *************** *@nntp6.u.washi ngton.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.co m

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************ ********@speake asy.net>,
"Steve @ Waypath" <st***@waypath. com> wrote:
"Donn Cave" <do**@u.washing ton.edu> wrote in message
news:do******** *************** *@nntp6.u.washi ngton.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.co m
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.washing ton.edu> wrote in message
news:do******** *************** *@nntp4.u.washi ngton.edu...
In article <5q************ ********@speake asy.net>,
"Steve @ Waypath" <st***@waypath. com> wrote:
"Donn Cave" <do**@u.washing ton.edu> wrote in message
news:do******** *************** *@nntp6.u.washi ngton.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.co m

Jul 18 '05 #10

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

Similar topics

2
5358
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 (Standard Exe) that calls and controls other modules (ActiveX-Exe). We decided for ActiveX-Exe because they run in different processes. That means if one module shuts down, the others are still alive. Second reasons
1
8884
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 commandline app, it will hang when it comes to the command-prompt (there's no return to send it to readline). Here's the code: public bool RunCommands(string cmds, ArrayList files) {
1
2040
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 thats calls a com+ application (- another project of mine that is in the same solution) and I want both of this "layers" to use my Log4Net Wrapper, and - here comes the tricky part - write to the same Log file. When the Com+ application is set to...
3
1234
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 tool was launched (Can we say Big Brother..). Some of the tools are very long running with lots of data others are small and very fast.. So I started down my traditional approach - Check google groups for the answer. I quickly became aware of...
0
1357
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 files and return text data. When I use this managed dll in my windows project, everything is working as intended and thus proving that both the .NET dll and the native dll works fine. When I use this dll in a ASP.NET web-application, the native...
7
4762
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 overrode the "ToString"). Using the "GetProcesses" method, I get Process, that I want to cast to _Process. So why do I get "System.InvalidCastException" all the time ? ("Specified cast is not valid") using System.Diagnostics;
2
2404
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 (dll's) I want these objects run in the background. I have used reflection to find the assembly and the object type. I want these objects to run separately to the windows service. Do i start these objects in a new thread or do they need to start...
7
8845
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 (2) a file. I threat them as two different problems for various reasons. The first problem involves the object writing to a file. The problem might be best explained by thinking of a single log file that's appended by different programs that...
0
195
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 running the process via SSH directly, write a small wrapper script which runs the process locally and saves the PID to a file on the machine (or prints it to stdout if the program itself has no output), and then run that wrapper remotely. For...
0
10412
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.