multiprocessing and process run time

T

Thomas Robitaille

Hi,

I'm making use of the multiprocessing module, and I was wondering if there
is an easy way to find out how long a given process has been running for.
For example, if I do

import multiprocessing as mp
import time

def time_waster():
time.sleep(1000)

p = mp.Process(target=time_waster)

p.start()

Is there a way that I can then find how long p has been running for? I
figured I can use p.pid to get the PID of the process, but I'm not sure
where to go from there. Is there an easy way to do this?

Thanks,

Thomas
 
O

OdarR

Hi,

I'm making use of the multiprocessing module, and I was wondering if there
is an easy way to find out how long a given process has been running for.
For example, if I do

import multiprocessing as mp
import time

def time_waster():
    time.sleep(1000)

p = mp.Process(target=time_waster)

p.start()

Is there a way that I can then find how long p has been running for? I
figured I can use p.pid to get the PID of the process, but I'm not sure
where to go from there. Is there an easy way to do this?

Something like :

import multiprocessing as mp
import time

q_out = mp.Queue()

def time_waster(qo):
time.sleep(5)
qo.put('stopped at ' + time.ctime(time.time()))

def main():
p = mp.Process(target=time_waster, args=(q_out,))
print "start at: " + time.ctime(time.time())
p.start()
p.join()
print q_out.get()

if __name__ == "__main__":
main()


The Queue (the one from multiprocessing ! don't mix with the Queue
module) is used as a safe exchange object between the parent and all
the child.

Olivier
 
P

Piet van Oostrum

Thomas Robitaille said:
TR> I'm making use of the multiprocessing module, and I was wondering if there
TR> is an easy way to find out how long a given process has been running for.
TR> For example, if I do
TR> import multiprocessing as mp
TR> import time
TR> def time_waster():
TR> time.sleep(1000)
TR> p = mp.Process(target=time_waster)
TR> p.start()
TR> Is there a way that I can then find how long p has been running for? I
TR> figured I can use p.pid to get the PID of the process, but I'm not sure
TR> where to go from there. Is there an easy way to do this?

You could look at the psutil module: http://code.google.com/p/psutil/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top