Tomasz Przemski
Tomasz Przemski

Reputation: 1127

Multiprocessing inside another function

I have such looped functions in this form:

from multiprocessing import Pool

def foo_1(i):
    count = 1
    for something:
        # blah blah blah
        h=0
        while len()<count:
          def foo_2(j):
              # blah blah blah
              return i + j
          h = h+1
        count +=1

if __name__ == '__main__':

    pool = Pool(4)
    pool.imap_unordered(foo_2, range()):

    pool.close()
    pool.join()

How should the syntax look like to make it work? Because giving if __name__ == '__main __' inside foo_1 does not work, and if I put it at the end, it does not recognize the functionfoo_2. Or maybe you need to completely rebuild the code syntax?

Upvotes: 0

Views: 1771

Answers (1)

Brent
Brent

Reputation: 196

I would change the structure of the code. Below is what I would do. Just curious, why do you want to use multi-processing on foo_2 vice foo_1?

from multiprocessing import Pool

# method broken out
def foo_2(j, i):
    # blah blah blah
    return i + j

def foo_1(i):
    count = 1
    for something:
        # blah blah blah
        h=0
        while len()<count:
          return_foo_2 = foo_2(j, i)
          h = h+1
        count +=1

if __name__ == '__main__':

    pool = Pool(4)

    # you will have to create a list of tasks to run
    # in your multiprocessing pool
    list_tasks = [(range(), range())]
    pool.imap_unordered(foo_2, list_tasks):

    pool.close()
    pool.join()

Upvotes: 2

Related Questions