我正在编写一个Python程序,并且我想同时运行两个while循环。我对Python很新,所以这可能是一个基本的错误/误解。该项目正在建立一个树莓派监控器,以确保它正在工作,如果没有,请发送电子邮件给指定的收件人。一个循环将与用户交互,并通过SSH实时响应发送给它的命令。并行while循环
while running is True:
user_input = raw_input("What would you like to do? \n").lower()
if user_input == "tell me a story":
story()
elif user_input == "what is your name":
print "Lancelot"
elif user_input == "what is your quest":
print "To seek the Holy Grail"
elif user_input == "what is your favorite color":
print "Blue"
elif user_input == "status":
if floatSwitch == True:
print "The switch is up"
else:
print "The switch is down"
elif user_input == "history":
print log.readline(-2)
print log.readline(-1) + "\n"
elif user_input == "exit" or "stop":
break
else:
print "I do not recognize that command. Please try agian."
print "Have a nice day!"
另一个循环将监视所有的硬件,并在出现错误时发送电子邮件。
if floatSwitch is True:
#Write the time and what happened to the file
log.write(str(now) + "Float switch turned on")
timeLastOn = now
#Wait until switch is turned off
while floatSwitch:
startTime = time.time()
if floatSwitch is False:
log.write(str(now) + "Float switch turned off")
timeLastOff = now
break
#if elapsedTime > 3 min (in the form of 180 seconds)
elif elapsedTime() > 180:
log.write(str(now) + " Sump Pump has been deemed broaken")
sendEmail("The sump pump is now broken.")
break
这两个函数都很重要,我希望它们能够并行运行,所以如何让它们像这样运行?感谢大家的帮助!
查看'multiprocessing'和/或'threading'模块。 – roippi