如何使用 Python 线程与线程之间的参数传递?
How can I use Python Threading with parameter passing between threads?
1.
总结问题
我正在使用 Python 2 和 Raspberry Pi 4 运行ning Raspbian Buster。试图限制我的程序从我的 Android 设备读取传感器数据的速度,该设备使用套接字通过 UDP 连接发送数据。 IE:我将其构建为一个小型激光绊线应用程序,开始时我想在每次传感器值低于特定数字时增加一个计数累加器,表示激光跳闸。但是,数据读取速度如此之快,以至于每次传感器液位低于该值时,计数都会增加很多次。我怎样才能每隔 X 秒只读取一次 UDP 数据?
2。
描述您尝试过的方法
- 我试过 time.sleep() 但据我所知,这是一个阻塞函数,经过测试,它确实阻止了我的程序通过 UDP 套接字更新传感器数据
- 我试过线程,但我不知道如何将传感器数据从一个线程传递到另一个线程。
- 我研究了 join() 但我碰巧遇到的技术文档对初学者不是很友好。
- 这里的一些帖子建议设置一个 client/server 界面,但我也不知道该怎么做。
3。
在适当的时候,显示一些代码
这是我目前在线程解决方案方面的进展。我正在使用名为 SensorUDP 的 android 应用程序,在打开环境光传感器并激活发送数据的情况下,该程序将读取 UDP 数据并打印出来。即使没有 UDP 数据,它仍然会 运行 计数线程。
import socket
from struct import *
import time
import threading
#We have 0-92 over a 1024 byte buffer representing distinct
#sensors being sent over UDP from my android smartphone
#this test program im only pulling byte 56 for Ambient Light sensing
#a laser shining into my phones sensor has 30,000-50,000 lums so anything
#less then that range must mean the laser was tripped
#UDP = SOCK_DGRAM, AF_INET = Internet
UDP_IP = "192.168.1.149" #my rpi4 address
print "Receiver IP: ", UDP_IP
UDP_PORT = 5000 #arbitrary for now but needs to match my androids broadcast
print "Port: ", UDP_PORT
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT)) #complete the binding
running = True
count = 0
laserOn = 1000 #using a low lums to test so I dont have to use laser every time
aLight = 0
result_available = threading.Event() #.set() this when the data is recieved
threadTimer = 5
def calcAmbLight():
# Continuously read from the UDP socket
while running:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
#the data from the app Im using to push the sensor data needs to
#be unpacked
aLight = "%.1f" %unpack_from ('!f', data, 56)
print ("Ambient Light = ", aLight)
def displayCount():
count = 0
while running:
time.sleep(1)
#how to pass aLight into this thread from the other thread?
if aLight < laserOn:
count = count + 1
print("Current aLight: ",aLight)
print("Current Count: ", count)
if __name__ == '__main__':
#create a basicthread to run calcAmbLight
calcAmb = threading.Thread(target= calcAmbLight)
calcAmb.start()
dispCount = threading.Thread(target = displayCount)
dispCount.start()
当前设置的问题是由于您没有在 calcAmbLight()
中将 aLight
声明为全局。将其声明为全局变量应该允许它工作。
def calcAmbLight():
global aLight # Set the global aLight, instead of a local stack variable
while running:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
#the data from the app Im using to push the sensor data needs to
#be unpacked
aLight = "%.1f" %unpack_from ('!f', data, 56)
print ("Ambient Light = ", aLight)
1. 总结问题 我正在使用 Python 2 和 Raspberry Pi 4 运行ning Raspbian Buster。试图限制我的程序从我的 Android 设备读取传感器数据的速度,该设备使用套接字通过 UDP 连接发送数据。 IE:我将其构建为一个小型激光绊线应用程序,开始时我想在每次传感器值低于特定数字时增加一个计数累加器,表示激光跳闸。但是,数据读取速度如此之快,以至于每次传感器液位低于该值时,计数都会增加很多次。我怎样才能每隔 X 秒只读取一次 UDP 数据?
2。 描述您尝试过的方法
- 我试过 time.sleep() 但据我所知,这是一个阻塞函数,经过测试,它确实阻止了我的程序通过 UDP 套接字更新传感器数据
- 我试过线程,但我不知道如何将传感器数据从一个线程传递到另一个线程。
- 我研究了 join() 但我碰巧遇到的技术文档对初学者不是很友好。
- 这里的一些帖子建议设置一个 client/server 界面,但我也不知道该怎么做。
3。 在适当的时候,显示一些代码 这是我目前在线程解决方案方面的进展。我正在使用名为 SensorUDP 的 android 应用程序,在打开环境光传感器并激活发送数据的情况下,该程序将读取 UDP 数据并打印出来。即使没有 UDP 数据,它仍然会 运行 计数线程。
import socket
from struct import *
import time
import threading
#We have 0-92 over a 1024 byte buffer representing distinct
#sensors being sent over UDP from my android smartphone
#this test program im only pulling byte 56 for Ambient Light sensing
#a laser shining into my phones sensor has 30,000-50,000 lums so anything
#less then that range must mean the laser was tripped
#UDP = SOCK_DGRAM, AF_INET = Internet
UDP_IP = "192.168.1.149" #my rpi4 address
print "Receiver IP: ", UDP_IP
UDP_PORT = 5000 #arbitrary for now but needs to match my androids broadcast
print "Port: ", UDP_PORT
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT)) #complete the binding
running = True
count = 0
laserOn = 1000 #using a low lums to test so I dont have to use laser every time
aLight = 0
result_available = threading.Event() #.set() this when the data is recieved
threadTimer = 5
def calcAmbLight():
# Continuously read from the UDP socket
while running:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
#the data from the app Im using to push the sensor data needs to
#be unpacked
aLight = "%.1f" %unpack_from ('!f', data, 56)
print ("Ambient Light = ", aLight)
def displayCount():
count = 0
while running:
time.sleep(1)
#how to pass aLight into this thread from the other thread?
if aLight < laserOn:
count = count + 1
print("Current aLight: ",aLight)
print("Current Count: ", count)
if __name__ == '__main__':
#create a basicthread to run calcAmbLight
calcAmb = threading.Thread(target= calcAmbLight)
calcAmb.start()
dispCount = threading.Thread(target = displayCount)
dispCount.start()
当前设置的问题是由于您没有在 calcAmbLight()
中将 aLight
声明为全局。将其声明为全局变量应该允许它工作。
def calcAmbLight():
global aLight # Set the global aLight, instead of a local stack variable
while running:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
#the data from the app Im using to push the sensor data needs to
#be unpacked
aLight = "%.1f" %unpack_from ('!f', data, 56)
print ("Ambient Light = ", aLight)