Python字典百万记录进程通过线程

时间:2016-01-12 20:32:00

标签: python multithreading python-2.7

Python字典包含需要由线程处理的数百万条记录增量订单。

data = {}
data[number] = name

这些数字按递增顺序排列,从1到100万

通过

调用威胁

第一个选项会发生逆火,因为它只处理max_tries定义的某些线程。

for i in xrange(1, max_tries + 1):
    name = data.get(str(i))

    current = MainThread(name)
    check_results.append(current)
    current.start()

第二个一次执行一百万条记录。

for item in data:
    name = data[item]

    current = MainThread(name)
    check_results.append(current)
    current.start()

你会如何解决它。

1 个答案:

答案 0 :(得分:0)

我会使用ThreadPoolExecutor。我还没有测试过以下代码,所以请将它们视为伪代码。

import concurrent.futures

def my_processing_function():
    pass

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:

    future_objects = []
    for item in data:
        name = data[item]
        future_objects.append(executor.submit(my_processing_function, name, 60))

    for future in concurrent.futures.as_completed(future_objects):
        try:
            res = future.result()
        except Exception as exc:
            print('exception: {}'.format(exc))
        else:
            print("Result: {}".format(res))