首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用在线程之间传递参数的Python线程?

如何使用在线程之间传递参数的Python线程?
EN

Stack Overflow用户
提问于 2020-05-27 02:16:35
回答 1查看 314关注 0票数 1

1.总结一下我正在使用的2和运行 Buster的Raspberry 4所遇到的问题。试图限制我的程序从我的Android设备读取传感器数据的速度,该设备使用Socket通过UDP连接发送数据。IE:我把它作为一个小的激光绊线应用程序来构建,为了开始,我想在传感器值降到某个数字以下的时候增加一个计数累加器,指示一个被绊倒的激光。但是,数据读取速度太快,每次传感器级别下降到以下,计数都会增加很多次。如何才能每隔X秒读取一次UDP数据?

2.描述您尝试过的

我尝试过使用

  • (),但从我所读到的情况来看,这是一个阻塞函数,经过测试后,它确实阻止了我的程序在UDP套接字
  • 上更新传感器数据--我已经尝试过线程处理,但我不知道如何将传感器数据从一个线程传递到另一个线程。我研究了join()的
  • ,但是我看到的技术文档并不是非常初级的

文章,这里推荐建立客户机/服务器接口,但我也不知道如何做到这一点。

3.在适当的情况下,向您展示一些代码,这是我目前在线程解决方案方面的进展。我正在使用名为SensorUDP的安卓应用程序,加上环境光传感器,启动并发送数据,这个程序将读取UDP数据并打印出来。即使没有UDP数据,它仍将运行计数线程。

代码语言:javascript
复制
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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-27 02:28:01

当前设置的问题是由于您没有在aLight中声明calcAmbLight()为全局的。将其声明为全局变量应允许此操作。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62034338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档