Editing RaspberryPi:ProgrammingInPython
Jump to navigation
Jump to search
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 257: | Line 257: | ||
For example, if you want to create a separate thread to fetch a sensor value five times, you could do it as follows: | For example, if you want to create a separate thread to fetch a sensor value five times, you could do it as follows: | ||
− | < | + | <pre>import threading |
import time | import time | ||
import RPi.GPIO as GPIO | import RPi.GPIO as GPIO | ||
Line 269: | Line 269: | ||
time.sleep(2) | time.sleep(2) | ||
− | if __name__ == | + | if __name__ == "__main__": |
setup() | setup() | ||
− | # Create a thread to call the function and pass | + | # Create a thread to call the function and pass "12" in as sensor pin |
x = threading.Thread(target=fetch_sensor_vals, args=(12,)) | x = threading.Thread(target=fetch_sensor_vals, args=(12,)) | ||
− | print( | + | print("Starting thread") |
x.start() | x.start() | ||
− | print( | + | print("Waiting for the thread to finish") |
x.join() | x.join() | ||
− | print( | + | print("Reading finished")</pre> |
− | </ | ||
− | |||
== Timed Thread Usage == | == Timed Thread Usage == | ||