babybigfox
Posts: 15
Joined: Wed Jun 03, 2015 4:13 pm

Do not wait for show message to finish

Fri Feb 26, 2016 4:52 pm

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

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Do not wait for show message to finish

Sat Feb 27, 2016 7:57 pm

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.

Return to “Astro Pi”