getpid Usage 
Author Message
 getpid Usage

Is there some convenient way of getting os.getpid to return the PID of
a given executable? What I want to do is start a process and know it's
PID. When I do the following I get the PID of the python executable.

os.system("path")
os.getpid()

Alternatively, as a hack, I am doing:
os.system(self.m_server_start_exe)
for line in os.popen("ps -ef | grep -i IMEController | awk '{ if (NR >
1) { print $2 }}'").readlines():
            pid = line[:-1]

I would rather just path in an executable name/pathname and have it
return this process' pid. Is there an updated version of os.getpid I
could use to do this. If not, is there a better solution that is not
shell dependent?

Thanks,
David



Mon, 30 Dec 2002 03:00:00 GMT  
 getpid Usage

Quote:

>Is there some convenient way of getting os.getpid to return the PID of
>a given executable? What I want to do is start a process and know it's
>PID. When I do the following I get the PID of the python executable.

>os.system("path")
>os.getpid()

>Alternatively, as a hack, I am doing:
>os.system(self.m_server_start_exe)
>for line in os.popen("ps -ef | grep -i IMEController | awk '{ if (NR >
>1) { print $2 }}'").readlines():
>            pid = line[:-1]

>I would rather just path in an executable name/pathname and have it
>return this process' pid. Is there an updated version of os.getpid I
>could use to do this. If not, is there a better solution that is not
>shell dependent?

Unless someone has an answer to this (I don't know of one),
if you have the source to the thing you are spawning, I recommend
writing out a PID file as part of that program (/tmp/my.pid,
whatever).


Tue, 31 Dec 2002 03:00:00 GMT  
 getpid Usage

You should have a look at the os.fork() function which is  python
"translation" of the fork system call (availability Unix).

It lets you create a new process (a child process) from the current one
(called the parent process). The child process execute the same than
the parent process. Thus, you have two processes, executing the same
code. The fork() function return 0 in the child process and the id
of the new created process for the parent process.

See the following example:

import os

i= os.fork()    # Create the new process

if i != 0:      # If i != 0, we are in the parent process
        print 'Forked process as the pid : ', i
else:           # We are in the child process
        os.system('ls -la')

hope-I-have-been-clear-enoughly-yours,

Gregoire Welraeds

Perceval Development team
-------------------------------------------------------------------------------
Perceval Technologies sa/nv     Tel: +32-2-6409194              
Rue Tenbosch, 9                 Fax: +32-2-6403154              


URL: http://www.perceval.be/
-------------------------------------------------------------------------------


Quote:
> Date: Thu, 13 Jul 2000 17:02:20 -0000


> Subject: getpid Usage

> Is there some convenient way of getting os.getpid to return the PID of
> a given executable? What I want to do is start a process and know it's
> PID. When I do the following I get the PID of the python executable.

> os.system("path")
> os.getpid()

> Alternatively, as a hack, I am doing:
> os.system(self.m_server_start_exe)
> for line in os.popen("ps -ef | grep -i IMEController | awk '{ if (NR >
> 1) { print $2 }}'").readlines():
>             pid = line[:-1]

> I would rather just path in an executable name/pathname and have it
> return this process' pid. Is there an updated version of os.getpid I
> could use to do this. If not, is there a better solution that is not
> shell dependent?

> Thanks,
> David

> --
> http://www.python.org/mailman/listinfo/python-list



Tue, 31 Dec 2002 03:00:00 GMT  
 getpid Usage

Quote:

> You should have a look at the os.fork() function which is  python
> "translation" of the fork system call (availability Unix).
[snip]
> i= os.fork()    # Create the new process

> if i != 0:      # If i != 0, we are in the parent process
>         print 'Forked process as the pid : ', i
> else:           # We are in the child process
>         os.system('ls -la')

But I think the original question requires use of one of the os.exec*
family rather than os.system - that way you're sure to know the PID of
the eventual target program and not the PID of the child process that
calls the target program, which isn't the same thing...

Martin.



Tue, 31 Dec 2002 03:00:00 GMT  
 getpid Usage
| Is there some convenient way of getting os.getpid to return the PID of
| a given executable? What I want to do is start a process and know it's
| PID. When I do the following I get the PID of the python executable.
|
| os.system("path")
| os.getpid()

Give me a break, in that example the "path" process should have exited
when control returns from the system() function, so there is no PID
left to report.  So the first question we have to ask is, what are
you really doing here?  Do you append an & to the command to get the
shell to run it in the background and exit?  Or does your particular
command do this on its own?

In the first case, there are a couple of things you can do.  You can
exec the command directly as the shell would do, rather than using
system() to invoke a shell.  The popen2 module does that, for an
example, and you can ignore the pipe, dup and close stuff there.
The parent fork will get the child's PID value from fork().  The
other option would be something like os.popen('cmd &\necho $!', 'r').
You will get back the PID as a text string in the pipe.

In the second case, there's nothing you can do without control over
the application, since that's where the fork you're interested in occurs.
As already suggested in another followup, in such a case the application
ought to write out its PID to a file, in the standard location for your
platform - /var/run, /etc, whatever.




Tue, 31 Dec 2002 03:00:00 GMT  
 getpid Usage

You should have a look at the os.fork() function which is  python
"translation" of the fork system call (availability Unix).

It lets you create a new process (a child process) from the current one
(called the parent process). The child process execute the same than
the parent process. Thus, you have two processes, executing the same
code. The fork() function return 0 in the child process and the id
of the new created process for the parent process.

See the following example:

import os

i= os.fork()    # Create the new process

if i != 0:      # If i != 0, we are in the parent process
        print 'Forked process as the pid : ', i
else:           # We are in the child process
        os.system('ls -la')

hope-I-have-been-clear-enoughly-yours,

Gregoire Welraeds

Perceval Development team
-------------------------------------------------------------------------------
Perceval Technologies sa/nv     Tel: +32-2-6409194
Rue Tenbosch, 9                 Fax: +32-2-6403154


URL: http://www.perceval.be/
-------------------------------------------------------------------------------


Quote:
> Date: Thu, 13 Jul 2000 17:02:20 -0000


> Subject: getpid Usage

> Is there some convenient way of getting os.getpid to return the PID of
> a given executable? What I want to do is start a process and know it's
> PID. When I do the following I get the PID of the python executable.

> os.system("path")
> os.getpid()

> Alternatively, as a hack, I am doing:
> os.system(self.m_server_start_exe)
> for line in os.popen("ps -ef | grep -i IMEController | awk '{ if (NR >
> 1) { print $2 }}'").readlines():
>             pid = line[:-1]

> I would rather just path in an executable name/pathname and have it
> return this process' pid. Is there an updated version of os.getpid I
> could use to do this. If not, is there a better solution that is not
> shell dependent?

> Thanks,
> David

> --
> http://www.python.org/mailman/listinfo/python-list



Tue, 31 Dec 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. getpid(), atoi(), time functions in Ada 95

2. Using os.getpid

3. os._exit(), os.kill(os.getpid(), 9)

4. VA processor usage at 10%

5. APL usage commercially

6. APL usage growth

7. Memory usage under J 5.1 /386 (PC)

8. Cpu usage

9. SocketReadStream nextLine usage?

10. Writing tight code to reduce CPU usage

11. ASMA030E Invalid literal usage

12. Usage survey

 

 
Powered by phpBB® Forum Software