简体   繁体   English

如何使用 Python 线程与线程之间传递参数

[英]How to use Python Threading with parameter passing between threads

long time lurker, first time poster.长期潜伏者,第一次海报。 Thank you for your time.感谢您的时间。

1. Summarize the problem I am working with Python 2 and a Raspberry Pi 4 running Raspbian Buster. 1. 总结我正在使用 Python 2 和运行 Raspbian Buster 的 Raspberry Pi 4 的问题 Trying to limit the speed at which my program reads sensor data from my Android device that is using Sockets to send its data via UDP connection.试图限制我的程序从使用 Sockets 通过 UDP 连接发送其数据的 Android 设备读取传感器数据的速度。 IE: I built this as a small laser trip wire app and to start I wanted to increment a count accumulator every time the sensor value dropped below a certain number, indicating a tripped laser. IE:我将其构建为一个小型激光绊线应用程序,并且开始时,我想在每次传感器值下降到某个数字以下时增加一个计数累加器,表明激光绊倒。 However, the data reads in so fast that count is incremented many times for each time the sensor level drops below.但是,数据读取速度如此之快,以至于每次传感器水平下降时,计数都会增加很多次。 How can I only read the UDP data every X seconds?我怎样才能每 X 秒读取一次 UDP 数据?

2. Describe what you've tried - I have tried time.sleep() but from what I have read, this is a blocking function and after testing, it does indeed stop my program being able to update the sensor data over the UDP socket - I have tried Threading but I cannot figure out how to pass in the sensor data from one thread into another. 2. 描述你尝试过的内容 - 我试过 time.sleep() 但从我读到的内容来看,这是一个阻塞 function 并且经过测试,它确实阻止了我的程序能够通过 UDP 套接字更新传感器数据- 我尝试过线程,但我无法弄清楚如何将传感器数据从一个线程传递到另一个线程。 - I researched join() but the technical documentation I happened across wasn't very beginner friendly. - 我研究了 join() 但我遇到的技术文档对初学者不太友好。 - Some posts here recomend setting up a client/server interface, but I am not sure how to do that either. - 这里的一些帖子建议设置客户端/服务器接口,但我也不知道该怎么做。

3. When appropriate, show some code Here is my current progress for a threaded solution. 3. 在适当的时候,显示一些代码这是我目前对线程解决方案的进展。 I am using the android app called SensorUDP and with the Ambient Light sensor on and Send Data activated this program will read in the UDP data and print it.我正在使用名为 SensorUDP 的 android 应用程序,并且在环境光传感器打开并激活发送数据的情况下,此程序将读取 UDP 数据并打印它。 even without the UDP data it will still run the count thread.即使没有 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()

The issue with the current setup is due to the fact that you are not declaring aLight as global in calcAmbLight() .当前设置的问题是由于您没有在calcAmbLight()中将aLight声明为全局。 Declaring it as a global variable should allow this to work.将其声明为全局变量应该允许它工作。

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)

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

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