Home How to run 2 functions with infinite loop at the same time in python
Reply: 0

How to run 2 functions with infinite loop at the same time in python

user907
1#
user907 Published in June 20, 2018, 7:25 am

I am coding for a smart traffic light. I am using Thread. The following is part of my code:

import RPi.GPIO as GPIO
import time
import signal
import sys
import thread

def StartTraffic():
# Loop forever
        while True:
               bla bla

def StartSensor():
        while True:
                if(GPIO.input(irs1)==True): #no traffic
                        print ("NO TRAFFIC")
                        time.sleep(1)
                if(GPIO.input(irs1)==False): #traffic
                        print ("GOT TRAFFIC")
                        time.sleep(1)
try:
        thread.start_new_thread(StartSensor())
        thread.start_new_thread(StartTraffic())

except:
        print "ERROR:unable to start thread"

When I run the code, it only run StartSensor(). How can I run StartSensor() and StartTraffic() at the same time?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.474874 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO