fluorine
fluorine

Reputation: 23

Multiprocessing using Python

I have two functions which are processing in parallel. I want to keep check about functions that when are they running and when finishing. How can I do that.I tried few things but it didn't work. My code looks like:

q1 = multiprocessing.Queue()
q2 = multiprocessing.Queue()
p1 = Process(target = fun1, args = (arg1,arg2,q1))
p2 = Process(target = fun2, args = (arg1,arg2,q2))
p1.start()
p2.start()

Basically trying to get a message like: 'fun1 is running', and after execution: 'fun1 ended, total time= t' and same for fun2 as well.

Upvotes: 0

Views: 74

Answers (1)

Icebreaker454
Icebreaker454

Reputation: 1071

You can use a decorator

Wrap your functions in a decorator that would to the thing for you.

import time

def timed(func):
    def _wrapper(*args, **kwargs):
        start = time.time()
        print(f"{func.__name__} has started")
        result = func(*args, **kwargs)
        end = time.time()
        print(f"{func.__name__} ended. Took: {end-start}s")
    
        return result
    return _wrapper


p1 = Process(target = timed(fun1), args = (arg1,arg2,q1))
p1.start()

Upvotes: 1

Related Questions