简体   繁体   English

如何在多处理中使用此功能?

[英]How can I use this function in multiprocessing?

I want to apply this function in multiprocessing:我想在多处理中应用这个函数:

import multiprocessing
import numpy as np

a = np.random.randint(1,4, size=(10,10,10))

def function(input_field,[num1,num2,num3]):
    pass

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=4)
    test_list = [(a,[27.5, 27.5, 25]), (b,[27.5, 27.5, 25]), (c,[27.5, 27.5, 25]), (d,[27.5, 27.5, 25])]
    results = pool.map(function, test_list)
    pool.close()
    pool.join()    

How can I apply function(input_field,[num1,num2,num3]) to multiprocessing?如何将function(input_field,[num1,num2,num3])应用于多处理?

You have 2 options based on the structure of your test_list :根据test_list的结构,您有 2 个选项:

  1. modify your function to support a single argument and unpack it inside the function and keep using map :修改您的函数以支持单个参数并将其解压缩到函数中并继续使用map

     def function(arg): input_field, l = arg
  2. use starmap and fix your function definition, because the structure of test_list is what starmap is expecting to get:使用starmap并修复您的函数定义,因为test_list的结构是starmap期望获得的:

     def function(input_field, l): ... results = pool.starmap(function, test_list)

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

相关问题 如何在生成器中使用 python 多处理? - How can I use python multiprocessing with generators? 如何在功能中使用多重处理? - How to use multiprocessing in function? 如何在python中使用multiprocessing.Pool.map实现对象中的功能? - how can i use multiprocessing.Pool.map in python for function in objects? 我可以在 Pool.imap 调用的 function 中使用多处理队列吗? - Can I use a multiprocessing Queue in a function called by Pool.imap? 如何使用多处理来更快地运行 function? - How can one use multiprocessing to run a run a function faster? 如何在Python 3中的异步处理IPC模块中使用asyncore? - How can I use asyncore with the multiprocessing IPC module in Python 3? 如何在Python中同时使用COM和多重处理? - How can I use COM and multiprocessing at the same time in Python? 在对传感器采样时,如何使用多重处理来绘制数据? - How can I use multiprocessing to plot data while sampling sensors? django 如何在管理命令中使用多处理 - django how can I use multiprocessing in a management command 如何在 python 中使用多处理来监控机器人 arm 的坐标? - How can I use multiprocessing in python to monitor the coordinates of a robotic arm?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM