【问题标题】:Python thread runs only once instead of running continuously. All threads should run continuouslyPython 线程只运行一次,而不是连续运行。所有线程都应该连续运行
【发布时间】:2022-01-17 05:41:31
【问题描述】:

我正在尝试实现线程,但它只运行一次而不是连续运行。

这是我创建线程和调用函数的地方:

thread_list = []
for ldx, stream in enumerate(input_stream_list):
   thread = threading.Thread(target=frame_thread, args=(stream, "stream{0}".format(ldx), ldx))
   thread_list.append(thread)

for thread in thread_list:
   thread.start()

retrieve_thread_list = []
for kdx, streamk in enumerate(input_stream_list):
   threadk = threading.Thread(target=retrieve_thread, args=(streamk, kdx))
   retrieve_thread_list.append(threadk)

for threadk in retrieve_thread_list:
   threadk.start()

for thread in thread_list:
   thread.join()

for threadk in retrieve_thread_list:
   threadk.join()

while True:
   pass

这是frame_thread的实现:-


def frame_thread(url, dire, i):
    global camera_reconnection, video_retrieval_status
    cap = cv2.VideoCapture(url)
    ret = cap.set(3, 768)
    ret = cap.set(4, 432)
    if not os.path.exists(name + dire):
        os.mkdir(name + dire)
    for x in os.listdir(name+dire):
        items[i].append(x)
    items[i].sort()
    for k in items[i]:
        q2[i].append(os.path.join(name+dire, k))
    while cap.isOpened():
        if camera_reconnection[i]:
            cap = cv2.VideoCapture(url)
            last_frame[i] = False
            camera_reconnection[i] = False
        if last_frame[i] is False:
            start_time = time.time()
            ret, frame = cap.read()
            if ret:
                video_file = os.path.join(name + dire, str(time.strftime('%H %M %S'))+".avi")
                q2[i].append(video_file)
                if len(q2[i]) > int(video_save):
                    data = q2[i].popleft()
                    os.remove(data)
                video_writer = cv2.VideoWriter(video_file, video_codec, fps, (int(cap.get(3)), int(cap.get(4))))
                while(int(time.time() - start_time)) < video_duration:
                    ret, frame = cap.read()
                    if not ret:
                        last_frame[i] = True
                        video_writer.release()
                        cv2.destroyAllWindows()
                        break
                    video_writer.write(frame)
                video_writer.release()
                if(video_retrieval_status[i] == VIDEO_LIST_RETRIEVAL_SPECIFIC_START):
                    video_retrieval_status[i] = VIDEO_LIST_RETRIEVAL_SPECIFIC_READY

这是我的retrieve_thread 函数:-

def retrieve_thread(streamk, k):
    global video_retrieval_status
    print("dbg0", flush=True)
    if(video_retrieval_status[k] == NO_RETRIEVAL):
        print("dbg1", flush=True)
    else:
        print("dbg2", flush=True)

        if (video_retrieval_status[k] == VIDEO_LIST_RETRIEVAL_SPECIFIC_READY):
            print("dbg3", flush=True)
            post_storage_retrieval_specific(k)
            print("dbg4", flush=True)
            video_retrieval_status[k] = NO_RETRIEVAL

threadk-retrieve_thread 只执行一次。我做错了什么?

【问题讨论】:

    标签: python python-3.x multithreading python-multithreading


    【解决方案1】:

    retrieve_thread() 没有任何循环。一旦函数结束,线程将关闭。

    尝试添加一个while循环:

    import time
    def retrieve_thread(streamk, k):
        global video_retrieval_status
        while True:  # Any condition to continue the thread execution
            time.sleep(5)  # Make sure the polling isn't too frequent
            print("dbg0", flush=True)
            if(video_retrieval_status[k] == NO_RETRIEVAL):
                print("dbg1", flush=True)
            else:
                print("dbg2", flush=True)
            
                if (video_retrieval_status[k] == VIDEO_LIST_RETRIEVAL_SPECIFIC_READY):
                    print("dbg3", flush=True)
                    post_storage_retrieval_specific(k)
                    print("dbg4", flush=True)
                    video_retrieval_status[k] = NO_RETRIEVAL
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多