2017-07-03 62 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) 
相關問題