Python:多线程复制 - 线程仍然存在
Python: Multithreading Copying - Threads are still alive
你好,我的问题是我有一个多线程复制 class。并且复制效果很好,但是程序不会退出,因为复制后线程仍然存在。我试图建立一个线程事件,但这没有效果。 t.join() 永无止境,因为线程还活着。我还把它们变成了守护进程,但这是不需要的,因为程序结束了,但是当程序停止时线程仍然存在。有谁知道这里出了什么问题? class 的输入是一个数据框,第一列是文件源,另一列是文件目标
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()
上被阻塞,这就是它不检查停止事件的原因。
解决这个问题最简单的方法是使线程成为守护线程。这样他们会在主线程终止时自动终止。
如果出于某种原因您不想to/can不这样做,那么您需要通过将特殊标记值插入工作线程将检查的队列来唤醒工作线程,如果工作人员从队列中看到这个值,它应该自行终止。
你好,我的问题是我有一个多线程复制 class。并且复制效果很好,但是程序不会退出,因为复制后线程仍然存在。我试图建立一个线程事件,但这没有效果。 t.join() 永无止境,因为线程还活着。我还把它们变成了守护进程,但这是不需要的,因为程序结束了,但是当程序停止时线程仍然存在。有谁知道这里出了什么问题? class 的输入是一个数据框,第一列是文件源,另一列是文件目标
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()
上被阻塞,这就是它不检查停止事件的原因。
解决这个问题最简单的方法是使线程成为守护线程。这样他们会在主线程终止时自动终止。
如果出于某种原因您不想to/can不这样做,那么您需要通过将特殊标记值插入工作线程将检查的队列来唤醒工作线程,如果工作人员从队列中看到这个值,它应该自行终止。