你好,我的问题是我有一个多线程复制类。复制工作正常,但程序不会退出,因为复制后线程仍处于活动状态。我试图构建一个线程事件,但没有效果。join()是永无止境的,因为线程是活动的。我还将它们设为daemonic,但这是不需要的,因为程序结束了,但当程序停止时线程仍处于活动状态。有人知道这里怎么了吗?类的输入是一个数据帧,第一列是文件源,另一列是文件目标
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)
编辑
如果我按键中断程序,它挂在这里:
<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:
工作线程在
self.fileQueue.get()
上被阻塞,这就是它不检查停止事件的原因。你知道吗解决这个问题最简单的方法是使线程成为守护线程。这样,当主线程终止时,它们将自动终止。你知道吗
如果出于某种原因您不想/不能这样做,那么您需要通过在队列中插入一个特殊的标记值来唤醒工作线程,您的工作线程将检查这个值,如果工作线程从队列中看到这个值,它应该自行终止。你知道吗
相关问题 更多 >
编程相关推荐