在Python中完成所有指令后停止一个线程

时间:2017-07-03 08:09:20

标签: python multithreading raspberry-pi

我是Python的新手,我正在尝试每秒创建一个将照片上传到服务器的线程。

这段代码应使用google.cloud库将照片上传到Google Cloud Platform。  我的问题是我想通过picamera每秒发送1帧。 没有线程,延迟太多了。使用下面的代码,它不是每隔一秒创建一个新线程,而是每次摄像机获得一个新帧。在完成所有操作后,它也不会“破坏”线程。你能帮我搞清楚吗?谢谢,抱歉我的英文和我的错误代码。

if int(round(time.time() * 1000)) - oldtime > 1000 & serConn:
            oldtime = time.time()
            thread = Thread(target = upload, args = (stream.read(), ))
            thread.start()
            thread.join()   

上传功能:

def upload(img):
    image = vision_client.image(content=img)

    # Performs label detection on the image file
    labels = image.detect_labels()
    for label in labels:
        if label.description == "signage":
            ser.write("0")
            print("Stop")
        else:
            ser.write("1")

1 个答案:

答案 0 :(得分:0)

我认为问题出在join函数调用上。这将导致调用块停止并等待线程终止。另外,我建议使用模块time在镜头之间休眠主线程。试试这个:

import time

while True:
    if serConn:
        thread = Thread(target = upload, args = (stream.read(), ))
        thread.start()
    time.sleep(1)
相关问题