raw_input stops thread?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbfreak
    New Member
    • Feb 2013
    • 1

    raw_input stops thread?

    Hello, im going out of my mind trying to solve this, im new at python and trying to code an alarm system with a raspberry pi.

    the alarm threads stops waiting for alarm2 threads raw_input, how can i solve this? Thanks in advance

    code below:

    Code:
    import time
    from threading import Thread
    from pyrowl import Pyrowl
    from datetime import datetime
    from espeak import espeak
    import piface.pfio as pfio
    import subprocess
    armed=0
    p=None
    pfio.init()
    def main2(keys):
        global p
        global res
        global cmess
        global armed
        pkey = None
        
        p = Pyrowl()
        if os.path.isfile("myproviderkey"):
            pkey = open("myproviderkey",'r').readline().strip()
            p.providerkey(pkey)
    
        p.addkey(keys)
        
        
        
    if __name__ == "__main2__":
        if os.path.isfile('myapikey'):
            keys = filter(None,
                          open("myapikey",'r').read().split("\n")
                          )
            
            main2(keys)
    
    
    
    
    def alarm1():
        global armed
        print "Thread running"
        #time.sleep(5)
        while True:        
            if armed==1:
                #while True:
                    #time.sleep(1)
                    print "Alarm active"
                    t2=datetime.now().strftime("%c")
                    if pfio.digital_read(1)==1:
                        print "Alarm! Groventre door open %s"%t2
                    continue
            else:
                print "inactive"
                time.sleep(1)
                continue
                
    
    def alarm2():
        global armed
        x=0
        textarm='"10 sekunder"'
        cards=["0008669047","0001479506"]
        print "Starting up...Disarmed\n"
        #while True:
        print "Waiting for card:\n"
        data=raw_input()
        if data in cards :
                    print "Valid card!"
                    if armed==0:
                        #espeak.synth("Card ok, armed in ten seconds")
                        #subprocess.call('espeak -s 110 -vsv ' +textarm, shell=True)
                        time.sleep(5)
                        armed=1
                        time.sleep(2)
                        print "Armed\n"                                  
                        #continue
                    if armed==1:
                        armed=0
                        time.sleep(2)
                        print "Disarmed\n"
                        
                        #t.join()                    
                        #continue
                        
                    
    def main():
        #armed=0
        global armed
        t=Thread(target=alarm1)
        t.setDaemon(True)
        t.start()
        t2=Thread(target=alarm2)
        t2.start()
        
        
    
    if __name__=='__main__':
        main()
    Last edited by bvdet; Feb 16 '13, 10:01 PM. Reason: Please use code tags when posting code. [code]....[/code]
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    alarm2 only asks for input once and then exits. It might also be a problem with the use of global armed. I use multiprocessing , not threading, but the threads may not communicate so changing something in one thread is not recognized in the other. It retains the value first passed. See Signaling Between Threads on Doug Hellmann's site as a place to start.

    Comment

    Working...