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?
P1 = Process(target = Timer(10), args = (10,)) P1.start() P2 = Process(target = Wait, args = (11,)) P2.start()Processobjects.