学习并行编程python [英] studying parallel programming python

查看:61
本文介绍了学习并行编程python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import multiprocessing
from multiprocessing import Pool
from source.RUN import*

def func(r,grid,pos,h):
    return r,grid,pos,h
    p = multiprocessing.Pool()  # Creates a pool with as many workers as you have CPU cores
    results = []


if __name__ == '__main__':
    for i in  pos[-1]<2:
    results.append(Pool.apply_async(LISTE,(r,grid,pos[i,:],h)))
    p.close()
    p.join()

for result in results:
    print('liste', result.get())

我想为(LISTE,(r,grid,pos[i,:],h))进程创建池,并且i在pos中,而pos在另一个文件中是变量,该文件是ndarray[],我必须在一个While循环之间的另一个文件中调用整个函数.但是这段代码给出了错误,如果我正在使用 if __name__ == '__main__': 它不会通过if __name__ == '__main__':模块下方 请告诉我我该怎么做

I want to create Pool for (LISTE,(r,grid,pos[i,:],h)) process and i is in pos which is variable in different file which is a ndarray[] and I have to call this whole function in another file in between one While Loop. but this code gives error and if I am using if __name__ == '__main__': it will not pass through below the if __name__ == '__main__': module please give me idea how I can make it

推荐答案

我仍然很难理解您的问题.但我认为这就是您要寻找的东西:

I'm still having a somewhat difficult time understanding your question. But I think this is what you're looking for:

您希望能够调用给定r, grid, pos, h遍历pos的池创建一个函数,将其馈送到Pool,然后返回结果.您还希望能够从不同的模块访问该功能.如果这是您要的内容,则可以这样操作:

You want to be able to call a function that creates a pool given r, grid, pos, h Iterate over pos feed it to the Pool, then return the results. You also want to be able to access that function from different modules. If that's what you're asking, you can do it like this:

async_module.py :

from multiprocessing import Pool

# Not sure where the LISTE function gets defined, but it needs to be in here.

def do_LISTE(*args):
    # args is a tuple containing (r, grid, pos[i, :], h)
    # we use tuple expansion (*args( to send each parameter to LISTE
    return LISTE(*args)

def async_process(r,grid,pos,h):
    return r,grid,pos,h
    p = multiprocessing.Pool()  # Creates a pool with as many workers as you have CPU cores
    results = p.map(do_LISTE, [(r,grid,pos[i,:], h) for i in pos[-1]<2])
    p.close()
    p.join()
    return results

然后在其他模块中

from async_module import async_process

def do_async_processing():
    r = "something"
    grid = get_grid()
    pos = get_pos()
    h = 345
    results = async_process(r, grid, pos, h)

if __name__ == "__main__":
    do_async_processing()  # Make sure the entry point is protected by `if __name__ == "__main__":`.

这篇关于学习并行编程python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆