2017-07-03 63 views
1

我是Python的新手,我试图每秒创建一个线程将照片上传到服务器。在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") 

回答

0

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

import time 

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