创建一个并行的' for'环?

时间:2017-05-20 14:38:05

标签: python python-2.7 parallel-processing joblib

我有一些代码循环通过字典,其中键是一个单词,它将术语频率分配给该键。 tok是来自某些文字的令牌列表。

def calculateTF(wordDict, tok):
    tfDict = {}
    termCount = len(tok)
    for word, count in wordDict.iteritems():
        tfDict[word] = count / float(termCount)
    return tfDict

我想分割迭代执行tfDict[word] = count / float(termCount)的wordDict的任务

1 个答案:

答案 0 :(得分:1)

Python中的multiprocessing模块兼容Unix和Windows,并提供远程和本地并发。 Global Interpreter Lock使用子进程而不是线程。通过创建“进程”对象然后调用其start()方法来生成进程。该流程遵循threading.Thread的API。

下面的示例演示了如何通过为每个函数创建一个进程来并行执行两个不同的函数,每个函数都有一个for循环。作为一个有趣的说明,每个进程都有自己的地址空间(virtual memory)。因此,程序变量不会在进程之间共享。为了说明这一点,我创建了两个全局变量,并将它们设置为当局部变量更新时应该采用的相反值。如您所见,它们保持相反,不会更新。您可以使用IPC,进程间通信,技术在两个进程之间共享数据。

<强>代码

import multiprocessing

global_any = False
global_all = True

def calculate_any(number):
    local_list_any = [1,2,3]
    local_result_any = any(True if i == number else False for i in local_list_any)
    global_any = local_result_any
    print("This is the first result within a process: {}".format(local_result_any))

def calculate_all(number):
    local_list_all = [1,2,3]
    local_result_all = all(True if i == number else False for i in local_list_all)
    global_all = local_result_all
    print("This is the second result within a process: {}".format(local_result_all))


if __name__ == "__main__":
    number = 2
    p1 = multiprocessing.Process(target = calculate_any, args = (number,))
    p2 = multiprocessing.Process(target = calculate_all, args = (number,))

    p1.start()
    p2.start()

    p1.join()
    p2.join()

    print('The result of the ANY global variable: {}'.format(global_any))
    print('The result of the ALL global variable: {}'.format(global_all))

<强>结果

The result of the ANY global variable: False
The result of the ALL global variable: True
This is the first result within a process: True
This is the second result within a process: False

<强>参考

https://docs.python.org/2/library/multiprocessing.html

https://pymotw.com/2/multiprocessing/basics.html

https://www.youtube.com/watch?v=Lu5LrKh1Zno