By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,592 Members | 975 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,592 IT Pros & Developers. It's quick & easy.

raw_input stops thread?

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1. import time
  2. from threading import Thread
  3. from pyrowl import Pyrowl
  4. from datetime import datetime
  5. from espeak import espeak
  6. import piface.pfio as pfio
  7. import subprocess
  8. armed=0
  9. p=None
  10. pfio.init()
  11. def main2(keys):
  12.     global p
  13.     global res
  14.     global cmess
  15.     global armed
  16.     pkey = None
  17.  
  18.     p = Pyrowl()
  19.     if os.path.isfile("myproviderkey"):
  20.         pkey = open("myproviderkey",'r').readline().strip()
  21.         p.providerkey(pkey)
  22.  
  23.     p.addkey(keys)
  24.  
  25.  
  26.  
  27. if __name__ == "__main2__":
  28.     if os.path.isfile('myapikey'):
  29.         keys = filter(None,
  30.                       open("myapikey",'r').read().split("\n")
  31.                       )
  32.  
  33.         main2(keys)
  34.  
  35.  
  36.  
  37.  
  38. def alarm1():
  39.     global armed
  40.     print "Thread running"
  41.     #time.sleep(5)
  42.     while True:        
  43.         if armed==1:
  44.             #while True:
  45.                 #time.sleep(1)
  46.                 print "Alarm active"
  47.                 t2=datetime.now().strftime("%c")
  48.                 if pfio.digital_read(1)==1:
  49.                     print "Alarm! Groventre door open %s"%t2
  50.                 continue
  51.         else:
  52.             print "inactive"
  53.             time.sleep(1)
  54.             continue
  55.  
  56.  
  57. def alarm2():
  58.     global armed
  59.     x=0
  60.     textarm='"10 sekunder"'
  61.     cards=["0008669047","0001479506"]
  62.     print "Starting up...Disarmed\n"
  63.     #while True:
  64.     print "Waiting for card:\n"
  65.     data=raw_input()
  66.     if data in cards :
  67.                 print "Valid card!"
  68.                 if armed==0:
  69.                     #espeak.synth("Card ok, armed in ten seconds")
  70.                     #subprocess.call('espeak -s 110 -vsv ' +textarm, shell=True)
  71.                     time.sleep(5)
  72.                     armed=1
  73.                     time.sleep(2)
  74.                     print "Armed\n"                                  
  75.                     #continue
  76.                 if armed==1:
  77.                     armed=0
  78.                     time.sleep(2)
  79.                     print "Disarmed\n"
  80.  
  81.                     #t.join()                    
  82.                     #continue
  83.  
  84.  
  85. def main():
  86.     #armed=0
  87.     global armed
  88.     t=Thread(target=alarm1)
  89.     t.setDaemon(True)
  90.     t.start()
  91.     t2=Thread(target=alarm2)
  92.     t2.start()
  93.  
  94.  
  95.  
  96. if __name__=='__main__':
  97.     main()
Feb 16 '13 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
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.
Feb 17 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.