Antivirusvivente
Posts: 130
Joined: Fri Dec 07, 2012 4:01 pm
Location: Bari
Contact: Website

Threading - Pausing thread1 while thread2 starts

Sat Apr 13, 2013 1:31 pm

Hi to all, I need help!

I'm running two different threads basically. The 1st one updates the time on the LCD display, the 2nd one detects if a button on the GPIO is pressed, and then makes some actions..

these are the modules:

1st one

Code: Select all

def Timer():
	data = ""
	sleep(1)
	while 1:
	    if (data!=str(datetime.now().strftime('%d %b %y  %H:%M:%S'))):
		data = str(datetime.now().strftime('%d %b %y  %H:%M:%S'))	
		bv4618.CursorRC(1,1)
		printer(data)
		bv4618.CursorRC(2,1)
		printer("Bari, " + str(temp.read_temp()))
2nd one:

Code: Select all

def my_service():
	v = False
	while 1:  
	    if ( GPIO.input(25) == False):
		v = not v
		block = True
		if v:
			bv4111.On("/dev/ttyAMA0",1,1)
			bv4618.Cmd(0x50)
			bv4618.CursorRC(1,1)
			printer("Luce accesa!")
			bv4618.Cmd(0x50)
		else:
			bv4111.On("/dev/ttyAMA0",1,0)
			bv4618.Cmd(0x50)
			bv4618.CursorRC(1,1)
			printer("Luce spenta!")
			bv4618.Cmd(0x50)
Now my program starts with:

Code: Select all

w = threading.Thread(name='worker', target=Timer)
w.start()
my_service()
I want block the execution on the 1st thread, because i want to show a different message on the LCD output for about 2 secs, and then re-start again the 1st thread. What should I do ( maybe avoiding classes and using a procedural language )
DomoHome is coming! - RaspberryPi ~ Raspbian ~ Android
https://www.facebook.com/pages/Domohome/155791057916884

timhoffman
Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: Threading - Pausing thread1 while thread2 starts

Sun Apr 14, 2013 11:38 am

HI

If you must use threads (I am not sure you do need them), consider changing your model to use a primary thread and a synchronized queue, to send messages to a another worker thread waiting/blocked on messages. That way whenever you want the LCD to change it's display send it a message (the text for instance) via the queue, on receiving the message it will wake up, update the LCD then wait for more messages.

See an example at the bottom.

http://docs.python.org/2/library/queue.html

Cheers

Tim

Return to “Python”