在另一个函数内部进行多处理

时间:2018-01-19 11:15:37

标签: python python-3.x multiprocessing

我有这种形式的循环函数:

odbc_connect()

语法应如何使其工作?因为在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() 内提供if __name__ == '__main __'不起作用,如果我将其放在最后,则无法识别函数foo_1。或者您可能需要完全重建代码语法?

1 个答案:

答案 0 :(得分:2)

我会改变代码的结构。以下是我要做的事情。只是好奇,你为什么要在foo_2副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()