我正在尝试完成有关线程模块的练习。在我的例子中,我只想创建只打印文件名的工人。
import optparse
import os
import queue
import threading
def main():
opts, args = parse_options()
filelist = get_files(args)
worker_queue= queue.Queue()
for i in range(opts.count):
threadnum = "{0}: ".format(i+1) if opts.debug else ""
worker = Worker(worker_queue, threadnum)
worker.daemon = True
worker.start()
for file in filelist:
worker_queue.put(file)
worker_queue.join()
class Worker(threading.Thread):
def __init__(self, worker_queue, threadnum):
super().__init__()
self.worker_queue = worker_queue
self.threadnum = threadnum
self.result = []
def run(self):
while True:
try:
file = self.worker_queue.get()
self.process(file)
finally:
self.worker_queue.task_done()
def process(self, file):
print("{0}{1}".format(self.threadnum, file))
def parse_options():
parser = optparse.OptionParser(
usage = "xmlsummary.py [options] [path] outputs a summary of the XML files in path; path defaults to .")
parser.add_option("-t", "--threads", dest = "count", default = 7,type = "int", help = ("the number of threads to use (1..20) [default %default]"))
parser.add_option("-v", "--verbose", default = False, action = "store_true", help = ("show verbose information if requested, [default %default]"))
parser.add_option("-d", "--debug", dest = "debug", default = False, action = "store_true", help = ("show debug information such as thread id, [default, %default]"))
opts, args = parser.parse_args()
if not (1 <= opts.count <= 20):
parser.error("threads must be in following range (1..20)")
return opts, args
def get_files(args):
filelist = []
for item in args:
if os.path.isfile(item):
filelist.append(item)
else:
for root, dirs , files in os.walk(item):
for file in files:
filelist.append(os.path.join(root, file))
return filelist
main()
此代码返回-d选项(将在输出中包含线程ID):
1: C:\P\1.jpg2: C:\P\2.jpg3: C:\P\3chapter.bat4: C:\P\423.txt5: C:\P\a.txt6: C:\P\bike.dat7: C:\P\binary1.dat
第一个问题是:
所有线程都打印在一行中,因为每个线程使用一个sys.stdout
?
我更改了以下命令的打印命令:
def process(self, file):
print("{0}{1}\n".format(self.threadnum, file))
现在我有以下结果:
1: C:\P\1.jpg
2: C:\P\2.jpg
3: C:\P\3chapter.bat
4: C:\P\423.txt
5: C:\P\a.txt
6: C:\P\bike.dat
7: C:\P\binary1.dat
1: C:\P\dckm.txt
2: C:\P\dlkcm.txt
3: C:\P\email.html
第二个问题是: 如何从输出中删除空行?
答案 0 :(得分:0)
您使用sys.stdout
走在正确的轨道上。解决这两个问题的简单解决方案就是像这样的函数
def tprint(msg):
sys.stdout.write(str(msg) + '\n')
sys.stdout.flush()
并使用它代替sys.stdout