【问题标题】:Multi-thread loop not ending in Python多线程循环未以 Python 结束
【发布时间】:2021-01-27 00:27:52
【问题描述】:

我编写(复制和改编)了一个 python 程序来做一些备份。它基本上从源位置读取文件并在目标位置复制或创建硬链接。它是多线程的,那部分工作正常。问题是线程中调用的循环永远不会结束。相关代码如下。我读到它可能是引发错误并挂起但不知道如何检查的线程之一。程序从文件队列中读取,处理文件,然后应该结束。它读取并处理文件,直到队列为空,然后挂起。关于尝试或查看什么的任何建议?希望我粘贴了足够多的代码来提供帮助

print ("beginning backup")
backup(path_to_source, path_to_full, dest_path, backup_type)
print ("Backup finished")

# --- clean full backup
if backup_type == "Full":
    print ("Cleaning up full backup - deleting deleted files")
    clean_backup()
    print ("Backup has been cleaned.  Done")

#--------------------------------------------------
def backup(path_to_source, path_to_full, dest_path, backup_type):
    threadWorkerCopyInc(source_files)
     print ("All Done")   #(never executes)

def threadWorkerCopyInc(fileNameList):
    global num_threads
    #fileQueue = queue.queue()
    for i in range(num_threads):
        t = threading.Thread(target=IncCopyWorker)
        t.daemon = True
        t.start()
        #threads.append(t)
    for fileName in fileNameList:
        fileQueue.put(fileName)
    fileQueue.join()
    #for i in range(len(threads)):
    #    fileQueue.put('None')
    #for t in threads:
    #    t.join()
    print('done with threadworkercopyinc')   #(never executes)


def IncCopyWorker():
    print ('Starting IncCopyWorker. ')   #executes
           
    while True:
        
        filename = fileQueue.get()
        if filename == 'None':
            print("\nFilename is none")  #executes when fileQueue.qsize() = 0
            
        with threadlock: processedfiles +=1
        print ("Files to go: %d  processed files:  %d  newfiles: %d  hardlinks: %d not copied: %d dfsr: %d  " %(fileQueue.qsize(), processedfiles, newfiles, hardlinks, notcopied, dfsr), end = '\r')
       
        is_dfsr = filename.upper().find("DFSR")
       
        if (is_dfsr == -1): 
              #do main processing here
        else:
            with threadlock: dfsr+=1
        fileQueue.task_done()
    print ("done with while true loop")  #never Executes

【问题讨论】:

  • 你的while True 循环应该如何结束?
  • 我知道我错过了一些代码。基本上在 while true 循环结束后,我希望它在我的代码中继续。我基本上希望它回到行打印(“全部完成”)。我实际上还有其他代码要在那里执行,所以如果我能到达那里,那就太好了。我要看看我是否可以编辑我的代码
  • 我理解的方式,当fileQueue为空时,线程将自动结束,当所有线程结束时,程序应该继续。我的基础是查看多个示例,所以我可能是错的。我尝试在 filename==none 块中进行中断,但它没有改变任何东西

标签: python multithreading queue python-multithreading


【解决方案1】:

所以看起来我的问题是我在我的 try/except 块中包含 continue 语句以尝试加快执行速度。当有 continue 或 break 语句时,循环永远不会结束。当我取出 continue 语句时,它完成了它应该的方式。我不知道原因 - 只是解决方案

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-08-07
    相关资源
    最近更新 更多