Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:
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())

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

share|improve this question

migrated from programmers.stackexchange.com Aug 7 '14 at 14:11

This question came from our site for professional programmers interested in conceptual questions about software development.

    
This: for i in pos[-1]<2: doesn't make sense. What do you actually mean there? – dano Aug 7 '14 at 15:06
    
for i in pos[-1]<2: in pos[-1]<2: is a ndarray ... – dbr Aug 7 '14 at 15:38
    
Maybe I'm missing something... isn't pos[-1]<2 just going to evaluate to True or False? So then you're doing for i in True: or for i in False:. – dano Aug 7 '14 at 15:41
    
no pos[-1]<2: has some value in array.. not Boolean..can I have your email I can send you something – dbr Aug 7 '14 at 15:45

1 Answer 1

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

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

Then in some other module:

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__":`.
share|improve this answer
    
thanks dano for your answer..still not get satisfactory result .can I have your email id so I can send you whole code for better idea..or [email protected] just ping me on this id.. – dbr Aug 11 '14 at 12:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.