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:
  
<syntaxhighlight lang="python" line='line'>import threading
+
<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__ == "__main__":
+
if __name__ == &quot;__main__&quot;:
 
     setup()
 
     setup()
     # Create a thread to call the function and pass "12" in as sensor pin
+
     # Create a thread to call the function and pass &quot;12&quot; in as sensor pin
 
     x = threading.Thread(target=fetch_sensor_vals, args=(12,))  
 
     x = threading.Thread(target=fetch_sensor_vals, args=(12,))  
     print("Starting thread")
+
     print(&quot;Starting thread&quot;)
 
     x.start()
 
     x.start()
     print("Waiting for the thread to finish")
+
     print(&quot;Waiting for the thread to finish&quot;)
 
     x.join()
 
     x.join()
     print("Reading finished")
+
     print(&quot;Reading finished&quot;)</pre>
</syntaxhighlight>
 
 
 
 
== Timed Thread Usage ==
 
== Timed Thread Usage ==
  

Please note that all contributions to UCT EE Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see UCT EE Wiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)