简体   繁体   English

Python 线程错误 - 使用 Raspberry Pi

[英]Python Threading Error - Using Raspberry Pi

建议修复后的新错误

 import fcntl, socket, struct, dweepy, time, platform, random, sqlite3 from grovepi import* potentiometer = 2 #A2 led = 5 #D5 ultrasonic_ranger = 4 #D4 dht_sensor_port = 7 #D7 Relay_pin = 2 #D2 from threading import Thread publisher_state = False publisherrotary_state = False publishersonic_state = False publisherhumidity_state = False def getTemp(): [temp,humidity] = dht(dht_sensor_port,0) return temp def getultrasonic(): distant = ultrasonicRead(ultrasonic_ranger) return distant def gethumidity(): [temp,humidity] = dht(dht_sensor_port,0) return humidity def getrotary(): i = analogRead(potentiometer) return i def listener(publisher): for dweet in dweepy.listen_for_dweets_from('PaulPi'): content = dweet["content"] should_publish = content["start"] print (should_publish) if should_publish == "true": # start the publisher thread global publisher_state publisher_state = True if not publisher.is_alive(): publisher = Thread(target=publisher_method_dan) publisher.start() else: publisher_state = False print ("wasn't true") def listenersonic(publishersonic): for dweet in dweepy.listen_for_dweets_from('PaulPi3'): content = dweet["content"] shouldsonic_publish = content["start"] print (shouldsonic_publish) if shouldsonic_publish == "true": # start the publisher thread global publishersonic_state publishersonic_state = True if not publishersonic.is_alive(): publishersonic = Thread(target=publisher_method_sonic) publishersonic.start() else: publishersonic_state = False print ("wasn't true") def listenerrotary(publisherrotary): for dweet in dweepy.listen_for_dweets_from('PaulPi5'): content = dweet["content"] shouldrotary_publish = content["start"] print (shouldrotary_publish) if shouldrotary_publish == "true": # start the publisher thread global publisherrotary_state publisherrotary_state = True if not publisherrotary.is_alive(): publisherrotary = Thread(target=publisher_method_rotary) publisherrotary.start() else: publisherrotary_state = False print ("wasn't true") def listenerhumidity(publisherhumidity): for dweet in dweepy.listen_for_dweets_from('PaulPi7'): content = dweet["content"] shouldhumidity_publish = content["start"] print (shouldhumidity_publish) if shouldhumidity_publish == "true": # start the publisher thread global publisherhumidity_state publisherhumidity_state = True if not publisherhumidity.is_alive(): publisherhumidity = Thread(target=publisher_method_humidity) publisherhumidity.start() else: publisherhumidity_state = False print ("wasn't true") def publish(): dict = {} dict["Temperature"] = 'getTemp' dweepy.dweet_for('PaulPi2', dict) def publishhumidity(): dict = {} dict["humidity"] = 'gethumidity' dweepy.dweet_for('PaulPi8', dict) def publishsonic(): dict = {} dict["ultrasonic"] = 'getultrasonic' dweepy.dweet_for('PaulPi4', dict) def publishrotary(): dict = {} dict["rotary"] = 'getrotary' dweepy.dweet_for('PaulPi6', dict) def publisher_method_dan(): while publisher_state: dict = {} dict["Temperature"] = getTemp() result = dweepy.dweet_for('PaulPi2', dict) print (result) time.sleep(5) print ("publishing ending") def publisher_method_humidity(): while publisherhumidity_state: dict = {} dict["humidity"] = gethumidity() result = dweepy.dweet_for('PaulPi8', dict) print (result) print ("publishing ending") def publisher_method_sonic(): while publishersonic_state: dict = {} dict["ultrasonic"] = getultrasonic() result = dweepy.dweet_for('PaulPi4', dict) print (result) print ("publishing ending") def publisher_method_rotary(): while publisherrotary_state: dict = {} dict["rotary"] = getrotary() result_rotary = dweepy.dweet_for('PaulPi6', dict) print (result_rotary) time.sleep(5) print ("publishing ending") publisher_thread = Thread(target=publisher_method_rotary) listener_thread = Thread(target=listenerrotary, args=(publisher_thread,)) listener_thread.start() publisher_thread = Thread(target=publisher_method_humidity) listener_thread = Thread(target=listenerhumidity, args=(publisher_thread,)) listener_thread.start() publisher_thread = Thread(target=publisher_method_sonic) listener_thread = Thread(target=listenersonic, args=(publisher_thread,)) listener_thread.start() publisher_thread = Thread(target=publisher_method_dan) listener_thread = Thread(target=listener, args=(publisher_thread,)) listener_thread.start()

I'm receiving the following error once i run the python code with my Raspberry Pi, any help would be appreciated as i cannot seem to figure it out.使用 Raspberry Pi 运行 python 代码后,我收到以下错误,任何帮助将不胜感激,因为我似乎无法弄清楚。

I am running this off of my Raspberry Pi and Android Phone我在我的 Raspberry Pi 和 Android 手机上运行这个

The Phone is being used to press buttons to start the sensors and to try receive information from dweet.io.电话用于按下按钮以启动传感器并尝试从 dweet.io 接收信息。 I can see the info on dweet.io but cannot pull the info back to the phone displaying the relevant information.我可以在 dweet.io 上看到信息,但无法将信息拉回显示相关信息的手机。

I am very new to python coding.我对python编码很陌生。

Python线程错误

You are getting error because you are trying to start the thread which had already started in the past.您收到错误,因为您正在尝试启动过去已经启动的线程。 If I'm understood correctly, I think you are intending to check whether a thread is alive or not and if it is not alive, you would like to restart it.如果我理解正确,我认为您打算检查一个线程是否处于活动状态,如果它不处于活动状态,您想重新启动它。

To accomplish this,为了实现这一点,

Replace a code like this:替换这样的代码:

if not publisherhumidity.is_alive():
    publisherhumidity = Thread(target=publisher_method_humidity)
publisherhumidity.start()

With:和:

if not publisherhumidity.is_alive():
    publisherhumidity = Thread(target=publisher_method_humidity)
    publisherhumidity.start() #--> place this statement inside if block

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM