Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中多处理或多线程的动态刷新打印_Python_Multithreading_Download - Fatal编程技术网

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

Python中多处理或多线程的动态刷新打印,python,multithreading,download,Python,Multithreading,Download,我已经实现了一个多处理下载程序。 如何打印可自动刷新的状态栏(完成率、下载速度) 在终端的不同部分 像这样: 499712 [6.79%] 68k/s // keep refreshing 122712 [16.79%] 42k/s // different process/thread 99712 [56.32%] 10k/s 代码: DownloadProcess继承Process类并触发download方法 我使用队列存储url。

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

像这样:

    499712  [6.79%]   68k/s     // keep refreshing
    122712  [16.79%]   42k/s    // different process/thread
     99712  [56.32%]   10k/s
代码:

DownloadProcess继承Process类并触发download方法

我使用队列存储url。这是一个开始的过程

  ...
  for i in range(3):
    t = DownloadProcess(queue)
    t.start()
    for url in urls:
        queue.put(url)
  queue.join()

下面是一个实现了多处理和多线程的演示。要尝试其中一种方法,只需取消对代码顶部的导入行的注释。如果单行上有进度条,则可以使用打印“\r”的技术将光标移回行的开头。但是,如果你想有多行进度条,那么你就必须有一个小爱好者。每次我想打印进度条时,我都会清除屏幕。看看这篇文章,它在生成下面的代码方面帮了我很大的忙。他展示了这两种技巧。您还可以尝试一下作为python标准库一部分的curses库。这个问题也提出了类似的问题。主线程/进程生成执行工作的子线程,并使用队列将其进度传回主线程。我强烈建议使用队列进行进程/线程间通信。然后,主线程显示进度并等待所有子线程结束执行,然后退出自身

代码

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


您是否可以提供come代码以供我们改进?可能会对您有所帮助,但仅限于为任务提供良好的可定位小部件。您需要有一个“head”来管理主进程中正在运行的所有列表。做一个网页可能会更容易,但我不想使用一个全功能的库。我只需要做一个简单的展示,绝对精彩。比我想象中的怪物要简单得多。谢谢。@Cerin Python几乎总是这样:)
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()