简体   繁体   English

Python:多线程复制-线程仍然存在

[英]Python: Multithreading Copying - Threads are still alive

Hello my problem is I have a multithreading copying class. 您好,我的问题是我有一个多线程复制类。 And the copying works well, but the program is dont quit because the threads are still alive after copying. 复制效果很好,但是该程序不会退出,因为复制后线程仍然存在。 I tried to build in a thread event, but this has no effect. 我试图建立一个线程事件,但这没有效果。 The t.join() is never ending because the threads are alive. t.join()永无休止,因为线程仍然存在。 I also turned them daemonic, but this is unwanted, because the program ends but the threads are still alive when the program stops. 我也将它们变成了守护进程,但这是不必要的,因为程序结束了,但是当程序停止时线程仍然处于活动状态。 Has anyone an idea what is wrong here? 有谁知道这是怎么回事? The Input of the class is a dataframe with the file source in first column and file destination in the other column 该类的Input是一个数据帧,第一列中的文件源,另一列中的文件目标

import shutil as sh
from multiprocessing import Queue, Process, Value, Lock, cpu_count 
import threading, os, time,queue


class ThreadedCopy():


totalFiles = 0
copyCount = 0
lock = threading.Lock()

def __init__(self,srcDst):        
    #fileList = srcDst['srcCol']
    self.fileQueue = queue.Queue()
    self.totalFiles = srcDst.shape[0]

    print(str(self.totalFiles) + " files to copy.")
    self.threadWorkerCopy(srcDst)


def CopyWorker(self):
    while True:
    #while True:
        fileRow = self.fileQueue.get()
        sh.copyfile(fileRow[1], fileRow[2])

        self.fileQueue.task_done()
        with self.lock:
            self.copyCount += 1
            percent = (self.copyCount * 100) / self.totalFiles
            if (percent%10==0):
                print(str(percent) + " percent copied.")

def threadWorkerCopy(self, srcDst):
    threads=[]
    for fileRow in srcDst.itertuples():
        self.fileQueue.put(fileRow)
    for i in range(cpu_count()):
        t = threading.Thread(target=self.CopyWorker,name='CopyThread')            
        t.daemon = True
        t.start()
        #threads.append(t)

    self.fileQueue.join()

ThreadedCopy(scrDstDf)

EDIT 编辑

If I key interupt the program It is hanging here: 如果我对程序进行交互操作,它就挂在这里:

<ipython-input-14-8d9a9b84e73f> in threadWorkerCopy(self, srcDst)
    380         self.stop_event.set()
    381         for thread in threads:
--> 382             thread.join()
    383 
    384 #ThreadedCopy(scrDstDf)

/usr/lib/python3.5/threading.py in join(self, timeout)
   1052 
   1053         if timeout is None:
-> 1054             self._wait_for_tstate_lock()
   1055         else:
   1056             # the behavior of a negative timeout isn't documented, but

/usr/lib/python3.5/threading.py in _wait_for_tstate_lock(self, block, timeout)
   1068         if lock is None:  # already determined that the C code is done
   1069             assert self._is_stopped
-> 1070         elif lock.acquire(block, timeout):
   1071             lock.release()
   1072             self._stop()

KeyboardInterrupt: 

Your worker thread is blocked on self.fileQueue.get() , that's why it's not checking the stop event. 您的工作线程在self.fileQueue.get()上被阻止,这就是为什么它不检查stop事件的原因。

The most easiest way to solve this is to make the thread a daemon thread. 解决此问题的最简单方法是使该线程成为守护线程。 That way they'll automatically get terminated when the main thread terminates. 这样,当主线程终止时,它们将自动终止。

If for some reason you don't want to/can't do this, then you'll need to wake up the worker thread by inserting a special marker value to the queue that your worker will check, if the worker sees this value from the queue, it should terminate itself. 如果由于某种原因您不想/不能这样做,那么您需要通过在工作人员将要检查的队列中插入一个特殊的标记值到工作人员将要检查的队列中来唤醒工作线程。队列,它应该终止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM