0

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?

1
  • The problem is that foo_2 is a completely different function on each iteration. And, even if you never call foo_1, you won't be able to access it, as it's defined on "runtime", in the same way you'd not be able to access any other private variable. Commented Jan 19, 2018 at 11:41

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.