有没有一种方法可以使用不同的参数自动多次调用函数?

时间:2020-05-09 22:04:18

标签: python python-multiprocessing

我正尝试每次针对具有不同args的函数启动多个进程,我想知道是否有更方便的方式编写此代码:

if __name__ == "__main__":    
    p1 = multiprocessing.Process(target=get_articles, args=[1, 101, 1])
    p2 = multiprocessing.Process(target=get_articles, args=[101, 201, 2])
    p3 = multiprocessing.Process(target=get_articles, args=[201, 301, 3])
    p4 = multiprocessing.Process(target=get_articles, args=[301, 401, 4])
    p5 = multiprocessing.Process(target=get_articles, args=[401, 501, 5])
    p6 = multiprocessing.Process(target=get_articles, args=[501, 601, 6])
    p7 = multiprocessing.Process(target=get_articles, args=[601, 701, 7])
    p8 = multiprocessing.Process(target=get_articles, args=[701, 801, 8])
    p9 = multiprocessing.Process(target=get_articles, args=[801, 901, 9])
    p10 = multiprocessing.Process(target=get_articles, args=[901, 1001, 10])
    p11 = multiprocessing.Process(target=get_articles, args=[1001, 1101, 11])
    p12 = multiprocessing.Process(target=get_articles, args=[1101, 1201, 12])
    p13 = multiprocessing.Process(target=get_articles, args=[1201, 1301, 13])
    p14 = multiprocessing.Process(target=get_articles, args=[1301, 1401, 14])
    p15 = multiprocessing.Process(target=get_articles, args=[1401, 1501, 15])
    p16 = multiprocessing.Process(target=get_articles, args=[1501, 1601, 16])
    p17 = multiprocessing.Process(target=get_articles, args=[1601, 1701, 17])



    p1.start()
    p2.start()
    p3.start()
    p4.start()
    p5.start()
    p6.start()
    p7.start()
    p8.start()
    p9.start()
    p10.start()
    p11.start()
    p12.start()
    p13.start()
    p14.start()
    p15.start()
    p16.start()
    p17.start()

2 个答案:

答案 0 :(得分:3)

processes = [ multiprocessing.Process(target=get_articles, args=[i*100-99, i*100+1, i]) for i in range(1,18) ]
for p in processes:
    p.start()

答案 1 :(得分:0)

要回答您的原始问题,partial模块中有一个名为functools的函数。它期望一个函数作为第一个参数,然后是在调用时传递给该函数的位置和关键字参数。它返回一个函数,该函数将同时使用预先保存的参数和您提供的任何新参数来执行。当您希望始终传递一些参数但更改其他参数时,这将很有帮助。

这里是partial的{​​{3}}。

这是一个例子:

from functools import partial

def exclaim(a, b):
    print("{} {}!".format(a, b))

# This is a function   
happy_birthday = partial(exclaim, "Happy Birthday")

happy_birthday("Bob")
happy_birthday("Alice")

在您的案例中使用partial,我想您可以使用类似这样的东西:

# Declare this outside of a loop
threaded_get_process = partial(Process, target=get_articles)

# Call this in a loop where you iterate over a list of the arguments to pass into the threaded call
threaded_get_process(args=arg_items)

但是,对于您的用例,有一种更简单的方法。 partial函数不是必需的。您可以仅遍历参数列表。

from multiprocessing import Process

def get_articles(one, two, three):
    # Just for testing. Replace with your actual function
    print("{} {} {}".format(one, two, three))

params = [
    [1, 101, 1],
    [101, 201, 2],
    [201, 301, 3],
    [301, 401, 4],
    [401, 501, 5],
    [501, 601, 6],
    [601, 701, 7],
    [701, 801, 8],
    [801, 901, 9],
    [901, 1001, 10],
    [1001, 1101, 11],
    [1101, 1201, 12],
    [1201, 1301, 13],
    [1301, 1401, 14],
    [1401, 1501, 15],
    [1501, 1601, 16],
    [1601, 1701, 17]
]

for param in params:
    new_thread = Process(target=get_articles, args=param)
    new_thread.start()