<i id='3Ty8k'><tr id='3Ty8k'><dt id='3Ty8k'><q id='3Ty8k'><span id='3Ty8k'><b id='3Ty8k'><form id='3Ty8k'><ins id='3Ty8k'></ins><ul id='3Ty8k'></ul><sub id='3Ty8k'></sub></form><legend id='3Ty8k'></legend><bdo id='3Ty8k'><pre id='3Ty8k'><center id='3Ty8k'></center></pre></bdo></b><th id='3Ty8k'></th></span></q></dt></tr></i><div id='3Ty8k'><tfoot id='3Ty8k'></tfoot><dl id='3Ty8k'><fieldset id='3Ty8k'></fieldset></dl></div>

    <small id='3Ty8k'></small><noframes id='3Ty8k'>

      <bdo id='3Ty8k'></bdo><ul id='3Ty8k'></ul>
    <legend id='3Ty8k'><style id='3Ty8k'><dir id='3Ty8k'><q id='3Ty8k'></q></dir></style></legend>

    1. <tfoot id='3Ty8k'></tfoot>

      使用 multiprocessing.pool.map 传递 kwargs

      时间:2023-05-26
        <tbody id='NeNZw'></tbody>
      <tfoot id='NeNZw'></tfoot>
        <i id='NeNZw'><tr id='NeNZw'><dt id='NeNZw'><q id='NeNZw'><span id='NeNZw'><b id='NeNZw'><form id='NeNZw'><ins id='NeNZw'></ins><ul id='NeNZw'></ul><sub id='NeNZw'></sub></form><legend id='NeNZw'></legend><bdo id='NeNZw'><pre id='NeNZw'><center id='NeNZw'></center></pre></bdo></b><th id='NeNZw'></th></span></q></dt></tr></i><div id='NeNZw'><tfoot id='NeNZw'></tfoot><dl id='NeNZw'><fieldset id='NeNZw'></fieldset></dl></div>

        <small id='NeNZw'></small><noframes id='NeNZw'>

          <legend id='NeNZw'><style id='NeNZw'><dir id='NeNZw'><q id='NeNZw'></q></dir></style></legend>
          • <bdo id='NeNZw'></bdo><ul id='NeNZw'></ul>

                本文介绍了使用 multiprocessing.pool.map 传递 kwargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我想使用 Pool.map() 将关键字参数传递给我的工作函数.在搜索论坛时,我找不到明确的例子.

                I would like to pass keyword arguments to my worker-function with Pool.map(). I can't find a clear example of this when searching forums.

                示例代码:

                import multiprocessing as mp
                
                def worker((x,y), **kwargs):
                    kwarg_test = kwargs.get('kwarg_test', False)
                    print("kwarg_test = {}".format(kwarg_test))     
                    if kwarg_test:
                        print("Success")
                    return x*y
                
                def wrapper_process(**kwargs):
                    jobs = []
                    pool=mp.Pool(4)
                    for i, n in enumerate(range(4)):
                        jobs.append((n,i))
                    pool.map(worker, jobs) #works
                    pool.map(worker, jobs, kwargs) #how to do this?   
                
                def main(**kwargs):
                    worker((1,2),kwarg_test=True) #accepts kwargs
                    wrapper_process(kwarg_test=True)
                
                if __name__ == "__main__":    
                    main()
                

                输出:

                kwarg_test = True
                Success
                kwarg_test = False
                kwarg_test = False
                kwarg_test = False
                kwarg_test = False
                TypeError: unsupported operand type(s) for //: 'int' and 'dict'
                

                类型错误与解析 multiprocessing.Pool 或 Queue 中的参数有关,我尝试了其他几种语法,例如制作 kwargs 列表;[kwargs, kwargs, kwargs, kwargs],以及多次尝试将 kwarg 包含在工作列表中,但没有运气.我从 map 到 map_async 跟踪 multiprocessing.pool 中的代码,并达到了task_batches = Pool._get_tasks(func, iterable, chunksize)当我遇到生成器结构时,在 pool.py 中.我很高兴将来能进一步了解这方面的信息,但现在我只是想了解一下:

                The type error has to do with parsing arguments inside of multiprocessing.Pool or Queue, and I have tried several other syntaxes, like making a list of the kwargs; [kwargs, kwargs, kwargs, kwargs], as well as several attempts to include the kwarg in the jobs list but no luck. I traced the code in multiprocessing.pool from map to map_async and got as far as task_batches = Pool._get_tasks(func, iterable, chunksize) in pool.py when I encountered the generator structure. I'm happy to learn more about this in future but for now I am just trying to find out:

                是否有允许通过 pool.map 传递 kwargs 的简单语法?

                Is there a simple syntax for allowing the passing of kwargs with pool.map?

                推荐答案

                如果您想遍历其他参数,请使用@ArcturusB 的答案.

                If you want to iterate over the other arguments, use @ArcturusB's answer.

                如果您只想传递它们,并且每次迭代都具有相同的值,那么您可以这样做:

                If you just want to pass them, having the same value for each iteration, then you can do this:

                from functools import partial
                pool.map(partial(worker, **kwargs), jobs)
                

                Partial 将参数绑定"到函数.但是,旧版本的 Python 不能序列化部分对象.

                Partial 'binds' arguments to a function. Old versions of Python cannot serialize partial objects though.

                这篇关于使用 multiprocessing.pool.map 传递 kwargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何终止多处理池进程? 下一篇:delay() 函数有什么作用(在 Python 中与 joblib 一起使

                相关文章

                最新文章

                1. <tfoot id='86kue'></tfoot>
                2. <i id='86kue'><tr id='86kue'><dt id='86kue'><q id='86kue'><span id='86kue'><b id='86kue'><form id='86kue'><ins id='86kue'></ins><ul id='86kue'></ul><sub id='86kue'></sub></form><legend id='86kue'></legend><bdo id='86kue'><pre id='86kue'><center id='86kue'></center></pre></bdo></b><th id='86kue'></th></span></q></dt></tr></i><div id='86kue'><tfoot id='86kue'></tfoot><dl id='86kue'><fieldset id='86kue'></fieldset></dl></div>
                    <bdo id='86kue'></bdo><ul id='86kue'></ul>

                3. <legend id='86kue'><style id='86kue'><dir id='86kue'><q id='86kue'></q></dir></style></legend>
                4. <small id='86kue'></small><noframes id='86kue'>