简体   繁体   English

使用Queue进行线程化并在Python中同时打印

[英]Threading with Queue and simultaneous print in Python

I'm trying to complete exercise about threading module. 我正在尝试完成有关线程模块的练习。 In my example, I just want to create workers which will just print filenames. 在我的示例中,我只想创建仅打印文件名的工作程序。

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()

This code returns me with -d option (which will include thread ID in output): 这段代码使用-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

The first question is: all threads print out in one line because each thread use one sys.stdout ? 第一个问题是:所有线程都打印在一行中,因为每个线程都使用一个sys.stdout

I have change print command with following: 我有以下更改打印命令:

def process(self, file):
print("{0}{1}\n".format(self.threadnum, file))

and now this I have following results: 现在,我得到以下结果:

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

The second question is: how to remove empty lines from the output? 第二个问题是:如何从输出中删除空行?

You are on the right track with the sys.stdout . 使用sys.stdout使您处在正确的轨道上。 A simple solutin to both problems would be a function like this 解决这两个问题的简单方法就是这样的功能

def tprint(msg):
    sys.stdout.write(str(msg) + '\n')
    sys.stdout.flush()

and use it instead sys.stdout 并改用sys.stdout

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

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