Python中多处理或多线程的动态刷新打印

Bob*_*Sun 8 python multithreading download

我已经实现了一个多处理下载器.如何打印可在终端的不同部分自动刷新的状态栏(完整速率,下载速度).

像这样:

    499712  [6.79%]   68k/s     // keep refreshing
    122712  [16.79%]   42k/s    // different process/thread
     99712  [56.32%]   10k/s
Run Code Online (Sandbox Code Playgroud)

码:

download(...)
...
    f = open(tmp_file_path, 'wb')
    print "Downloading: %s Bytes: %s" % (self.file_name, self.file_size)
    file_size_dl = 0
    block_sz = 8192
    start_time = time.time()
    while True:
        buffer = self.opening.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        end_time = time.time()
        cost_time = end_time - start_time
        if cost_time == 0:
            cost_time = 1
        status = "\r%10d  [%3.2f%%]  %3dk/s" % (file_size_dl,
                file_size_dl * 100. / self.file_size,
                file_size_dl * 100. / 1024 / 1024 / cost_time)
        print status,
        sys.stdout.flush()
    f.close()
Run Code Online (Sandbox Code Playgroud)

DownloadProcess继承Process类并触发下载方法.

我使用队列来存储网址.这是开始的过程

  ...
  for i in range(3):
    t = DownloadProcess(queue)
    t.start()
    for url in urls:
        queue.put(url)
  queue.join()
Run Code Online (Sandbox Code Playgroud)

Mar*_*agh 22

下面是一个实现了多处理和多线程的演示.要尝试其中一个,只需取消注释代码顶部的导入行.如果您在一行上有一个进度条,那么您可以使用打印'\ r'的技术将光标移回到行的开头.但是如果你想拥有多线进度条,那么你将不得不获得一点点发烧友.我每次想要打印进度条时都清除了屏幕.在Python上查看Unix上的文章控制台输出,它帮助我在下面生成代码.他展示了这两种技巧.你也可以给作为python标准库一部分的curses库.Multiline进度条问题类似的问题.主线程/进程生成执行工作的子线程,并使用队列将其进度传回主线程.我强烈建议使用队列进行进程间/线程通信.然后主线程显示进度并等待所有子进程在退出之前结束执行.

import time, random, sys, collections
from multiprocessing import Process as Task, Queue
#from threading import Thread as Task
#from Queue import Queue

def download(status, filename):
    count = random.randint(5, 30)
    for i in range(count):
        status.put([filename, (i+1.0)/count])
        time.sleep(0.1)

def print_progress(progress):
    sys.stdout.write('\033[2J\033[H') #clear screen
    for filename, percent in progress.items():
        bar = ('=' * int(percent * 20)).ljust(20)
        percent = int(percent * 100)
        sys.stdout.write("%s [%s] %s%%\n" % (filename, bar, percent))
    sys.stdout.flush()

def main():
    status = Queue()
    progress = collections.OrderedDict()
    workers = []
    for filename in ['test1.txt', 'test2.txt', 'test3.txt']:
        child = Task(target=download, args=(status, filename))
        child.start()
        workers.append(child)
        progress[filename] = 0.0
    while any(i.is_alive() for i in workers):
        time.sleep(0.1)
        while not status.empty():
            filename, percent = status.get()
            progress[filename] = percent
            print_progress(progress)
    print 'all downloads complete'

main()
Run Code Online (Sandbox Code Playgroud)

演示

在此输入图像描述