multiprocessing and process run time

OdarR Olivier.Darge at gmail.com
Fri Jun 19 17:24:39 CEST 2009


On 19 juin, 16:40, Thomas Robitaille <thomas.robitai... at gmail.com>
wrote:
> 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



More information about the Python-list mailing list