berry150
Posts: 8
Joined: Wed Feb 26, 2014 2:39 am

Execute 2 tasks in parallel (one task depends on the other)

Thu Mar 06, 2014 11:11 pm

Hi,

let's suppose I have two functions:

Code: Select all

def getTemperature():
	# prints a temperature every minute

def turnOnLED():
	# turns on a LED for 10min then turns it off
I'd like to write a python code which

- prints the temperature every minute independently of the LED's state
- turns on the LED for 10min if the temperature crosses a threshold, say 30°C.

A program like

Code: Select all

while True:	
	getTemperature()
	if getTemperature() > 30:
		turnOnLED()
isn't acceptable because getTemperature() stops printing its values till the LED is turned off.
I should certainly use multiprocessing or threading, but I'm too much of a noob to figure out how to write the code...Could someone help me?
Thanks in advance.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Execute 2 tasks in parallel (one task depends on the oth

Fri Mar 07, 2014 8:23 am

Something like?

Code: Select all

import time

keep_looping = True
LED_on = False
printed_time=time.time() - 60
while keep_looping:

   temp = get_temperature()

   if (time.time() - printed_time) >= 60:
      print temp
      printed_time = time.time()

   if temp >= 30.0:
      LED_switch_on_time = time.time()

      if not LED_on:
         switch_LED_on()
         LED_on = True

   else: # temperature < 30.0
      if LED_on:
         if (time.time() - LED_switch_on_time) > (10*60):
            switch_LED_off()
            LED_on = False

   time.sleep(0.1)


PiGraham
Posts: 3971
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Execute 2 tasks in parallel (one task depends on the oth

Fri Mar 07, 2014 9:33 am

berry150 wrote:Hi,

let's suppose I have two functions:

Code: Select all

def getTemperature():
	# prints a temperature every minute

def turnOnLED():
	# turns on a LED for 10min then turns it off
...

getTemperature() stops printing its values till the LED is turned off.]/quote]

The trick is to not use 'busy waits' (e.g. Sleep() or while() ) that stay in a function for a long time. Instead use a 'task loop' / 'round-robin task scheduler' that calls various tasks to do a little bit when the time is right.
That is what Joan has demonstrated:

Code: Select all

if (time.time() - printed_time) >= 60:
      print temp
      printed_time = time.time()
Keep testing the time and do something if the time is right, otherwise return to the main loop to allow other things to happen.
berry150 wrote:I should certainly use multiprocessing or threading, but I'm too much of a noob to figure out how to write the code...Could someone help me?
Thanks in advance.

You can use threads for this sort of thing, but you don't need to, and things get much more complicated. The difference is the threading can switch from one task to another when in the middle of a task. You could be half way through reading the temperature when the thread to use the temperature value to control an output starts to run. You have to add code to coordinate the different threads. With the 'task loop' your program does one thing after another in a defined sequence. If you are reading temperature that will complete before you get back to the main loop and go on to test the temperature, when the time is right. You should really understand how to get a computer to lots of things at the right times without 'blocking' (e.g. waiting for a minute in the temperature function) before you tackle threading.

berry150
Posts: 8
Joined: Wed Feb 26, 2014 2:39 am

Re: Execute 2 tasks in parallel (one task depends on the oth

Sat Mar 08, 2014 3:26 am

Thank you for your posts.... and the code works wonderfully.

Return to “Python”