小编Bob*_*Sun的帖子

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

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

像这样:

    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. / …
Run Code Online (Sandbox Code Playgroud)

python multithreading download

8
推荐指数
1
解决办法
3666
查看次数

标签 统计

download ×1

multithreading ×1

python ×1