如何从所有并行线程获取总数?这样,变量 ii 就会汇总到所有流中,并且总值始终显示在屏幕上。
class MThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while True:
file_in = self.queue.get()
self.get_check(file_in)
self.queue.task_done()
def get_check(self, file_in):
ii = 0
for item_ii in range(5):
ii = ii + 1
print('Total number of files in all streams %s = %s' % (file_in, ii))
def main(list_files):
queue = Queue()
for i in range(5):
t = MThread(queue)
t.setDaemon(True)
t.start()
for item in list_files:
queue.put(item)
queue.join()
if __name__ == "__main__":
list_file = []
main(list_file)