栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

星图结合tqdm?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

星图结合tqdm?

不可能

starmap()
,但是添加补丁是可能的
Pool.istarmap()
。它基于的代码
imap()
。您要做的就是创建
istarmap.py
-file并导入模块以应用补丁,然后再进行常规的multiprocessing-
imports。

Python <3.8

# istarmap.py for Python <3.8import multiprocessing.pool as mppdef istarmap(self, func, iterable, chunksize=1):    """starmap-version of imap    """    if self._state != mpp.RUN:        raise ValueError("Pool not running")    if chunksize < 1:        raise ValueError( "Chunksize must be 1+, not {0:n}".format(     chunksize))    task_batches = mpp.Pool._get_tasks(func, iterable, chunksize)    result = mpp.IMapIterator(self._cache)    self._taskqueue.put(        ( self._guarded_task_generation(result._job,         mpp.starmapstar,         task_batches), result._set_length        ))    return (item for chunk in result for item in chunk)mpp.Pool.istarmap = istarmap

Python 3.8以上

# istarmap.py for Python 3.8+import multiprocessing.pool as mppdef istarmap(self, func, iterable, chunksize=1):    """starmap-version of imap    """    self._check_running()    if chunksize < 1:        raise ValueError( "Chunksize must be 1+, not {0:n}".format(     chunksize))    task_batches = mpp.Pool._get_tasks(func, iterable, chunksize)    result = mpp.IMapIterator(self)    self._taskqueue.put(        ( self._guarded_task_generation(result._job,         mpp.starmapstar,         task_batches), result._set_length        ))    return (item for chunk in result for item in chunk)mpp.Pool.istarmap = istarmap

然后在您的脚本中:

import istarmap  # import to apply patchfrom multiprocessing import Poolimport tqdmdef foo(a, b):    for _ in range(int(50e6)):        pass    return a, bif __name__ == '__main__':    with Pool(4) as pool:        iterable = [(i, 'x') for i in range(10)]        for _ in tqdm.tqdm(pool.istarmap(foo, iterable),     total=len(iterable)): pass


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/661322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号