简体   繁体   English

将 ProgressBar 添加到多线程 Python 脚本

[英]Adding a ProgressBar to a multithreaded Python script

i am trying to add a progressbar to my script but i couldn't succeed because i think it is multi-threaded or maybe it should be added in a separate thread.我正在尝试将进度条添加到我的脚本中,但我无法成功,因为我认为它是多线程的,或者应该将它添加到单独的线程中。 i found plenty of solutions in stackoverflow, for example tqdm library but i couldn't implement it, also i think i have a confusion where exactly i have to implement the progress bar code to make it works.我在 stackoverflow 中找到了很多解决方案,例如 tqdm 库,但我无法实现它,而且我认为我有一个困惑,我必须实现进度条代码才能使其工作。

this is my code:这是我的代码:

# -*- coding: utf-8 -*
from __future__ import unicode_literals
# !/usr/bin/python
import  codecs
from multiprocessing.dummy import Pool

start_raw = "myfile"
threads = 10

with codecs.open(start_raw, mode='r', encoding='ascii', errors='ignore') as f:
    lists = f.read().splitlines()
    lists = list((lists))


def myfunction(x):
    try:
        print x

    except:
        pass


def Main():
    try:
        pp = Pool(int(threads))
        pr = pp.map(myfunction, lists)


    except:
        pass


if __name__ == '__main__':
    Main()




i have tried this solution https://stackoverflow.com/a/45276885/9746396 :我已经尝试过这个解决方案https://stackoverflow.com/a/45276885/9746396

# -*- coding: utf-8 -*
from __future__ import unicode_literals
# !/usr/bin/python
import  codecs
from multiprocessing.dummy import Pool
import tqdm

start_raw = "myfile"
threads = 1

with codecs.open(start_raw, mode='r', encoding='ascii', errors='ignore') as f:
    lists = f.read().splitlines()
    lists = list((lists))


def myfunction(x):
    try:
        print (x)

    except:
        pass

def Main():
    try:
        pp = Pool(int(threads))
        pr = pp.map(myfunction, lists)


    except:
        pass


if __name__ == '__main__':
    with Pool(2) as p:
        r = list(tqdm.tqdm(p.imap(Main(), range(30)), total=30))

but code does not run and i get exception (TypeError: 'NoneType' object is not callable)但代码没有运行,我得到异常(TypeError:'NoneType' object 不可调用)

0%|                                                                                           | 0/30 [00:00<?, ?it/s]Traceback (most recent call last):
  File "file.py", line 35, in <module>
    r = list(tqdm.tqdm(p.imap(Main(), range(30)), total=30))
  File "C:\mypath\Python\Python38-32\lib\site-packages\tqdm\std.py", line 1118, in __iter__
    for obj in iterable:
  File "C:\mypath\Python\Python38-32\lib\multiprocessing\pool.py", line 865, in next
    raise value
  File "C:\mypath\Python\Python38-32\lib\multiprocessing\pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
TypeError: 'NoneType' object is not callable
  0%|                                                                                           | 0/30 [00:00<?, ?it/s]


I presume you wanted to pass myfunction instead of Main to imap , consistently with the first example.我假设您想将myfunction而不是Main传递给imap ,与第一个示例一致。

When you pass Main() to p.imap in r = list(tqdm.tqdm(p.imap(Main(), range(30)), total=30)) , Python calls executes Main method and passes the return value as the first argument to imap .当您在r = list(tqdm.tqdm(p.imap(Main(), range(30)), total=30)) Main()传递给p.imap时, Python 调用执行Main方法并将返回值传递为imap的第一个参数。

You should remove the parentheses after Main as: p.imap in r = list(tqdm.tqdm(p.imap(Main, range(30)), total=30)) .您应该删除Main之后的括号: p.imap in r = list(tqdm.tqdm(p.imap(Main, range(30)), total=30))

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

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