1

I'm struggling to run 2 functions at the same time using Multiprocessing's Process function using the code below. My effort in the end is to running a timer that will be checked against from within the main body of the script. If the time has completed than the dependent in the main body will trigger its own action. If the time has not completed it will pass on to the next action.

I'm running this on a MacBook Pro with the build: Processor Name: Intel Core 2 Duo Processor Speed: 2.66 GHz Number of Processors: 1 Total Number of Cores: 2

Script:

#!/usr/bin/pyton

# MODULES
import time
from multiprocessing import Process

# GLOBAL VARIABLES
Completion = ''

# FUNCTIONS
def Timer(duration):
        global Completion
        Ticker = 0
        while Ticker != duration:
                Ticker = Ticker + 1
                print(Ticker)
                time.sleep(1)

        Completion = '{0}TickerDone'.format(duration)

def Wait(seconds):
        time.sleep(seconds)

#MAIN
P1 = Process(target = Timer(10))
P1.start()

P2 = Process(target = Wait(11))
P2.start()

P1.join()
P2.join()

print(Completion)

if Completion == '10TickerDone':
        print('Good to go!\n') 
else:   
        print('Not yet!\n')

# END

The two functions do what is excepted, however the second function, which is intended to wait 10+1 secondards for the completion of the first function, only begins once the first function, the 10 seconds timer, is completed. So, in essense, I have to wait 21 seconds to test a 10 second timer.

The results:

$ python Test.py 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
10TickerDone 
Good to go!

What I need to do is run these two functions in parallel so the second function can test if the first function is completed before executing a third function.

Where am I going wrong?

5
  • Note, I attempting the same thing with threading, replacing Process with Thread, but have the same results. Commented Nov 8, 2016 at 20:17
  • Hi Mark, I did try writing it out as you have it but I'm getting the same results. Commented Nov 8, 2016 at 20:21
  • P1 = Process(target = Timer(10), args = (10,)) P1.start() P2 = Process(target = Wait, args = (11,)) P2.start() Commented Nov 8, 2016 at 20:21
  • @danjmw: this is wrong in the same way. Look my answer bellow: you can't cal the functions when you create the Process objects. Commented Nov 8, 2016 at 20:23
  • Thanks for the feedback, but I'm not following (my bad)... Commented Nov 8, 2016 at 20:24

1 Answer 1

1

Your main problem there is that you are not running your target funcitons in the subprocesses, you are running then directly, before starting another process.

When you do code like: P1 = Process(target = Timer(10)) Python will imediattely resolve the expression to the left of the target=part - and call the function, wich will wait for 10 seconds, and then, create a subprocess object which target is actually the return value of that function (None).

So, if you just do:

#MAIN

P1 = Process(target=Timer, args=(10,))
P1.start()

P2 = Process(target=Wait, args=(11,))
P2.start()

You will start to see things going in your desired direction. Look at the difference: here I use the function names (which, by convention could be lowercase, btw), without an immediate "open parentheses" after the name. The parentheses following a variable or function name in Python denotes a synchronous object call (function call in this case). The use of the same name without the parentheses passes the function as a parameter for the Process object you are creating. The arguments are passed separated in the args parameter.

But besides that, multiprocessing does not work at all with global variables - as the name indicates, each function is run in a different process and has its own set of global variables).

Instead, you have to do something a little bit more sophisticated and use a (multiprocessing) Queue, passed as a parameter for each entry-point in a sub-process, to coordinate the actions for your parallel code.

Check the documentation at https://docs.python.org/2/library/multiprocessing.html#pipes-and-queues

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, jsbueno. My last comment, where I included the () after the first function was my bad, typing too fast. Thanks for the link. I'm sure you're correct. I'll take a moment to read through the doc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.