Page 1 of 1

Do not wait for show message to finish

Posted: Fri Feb 26, 2016 4:52 pm
by babybigfox
I have a program that relies on the joystick always being active, and if I call the function show_message(), it stops the system till the text has finished scrolling. Is there a way to stop it doing this and do it in the background? Thanks

Re: Do not wait for show message to finish

Posted: Sat Feb 27, 2016 7:57 pm
by Davespice
Hi there, I remember you from last time! You want to look into the Python threading module. This allows you to kick off a function and return immediately to go onto the next line of code leaving the function to run in the background.

Example:

Code: Select all

import time
import threading as th
from sense_hat import SenseHat

sense = SenseHat()

def message_shower(message):
    global sense
    sense.show_message(message)

def show_message_background(message):
    th.Thread(target=message_shower, args=(message,)).start()

show_message_background("my name is Dave")

while True:
    print("sleeping...")
    time.sleep(1)
I hope this helps.