多处理/多线程3 Python嵌套循环

时间:2018-05-24 15:00:21

标签: python multithreading python-2.7 multiprocessing nested-loops

我有以下代码的片段:

for x in xrange(100,14000):
    for y in xrange(0,3000):
        for z in xrange(0,700):
            operationg_tag = "%s-%s-%s" % (x,y,z)
            operation_code = ....

            and more bunch of code is running here on the tag generated above

我在快速良好的硬件中运行此代码,但这些循环的性质很慢,因为没有在多处理/多线程中运行

在每个标签上执行所需的代码大约需要0.4秒。

如何在这些嵌套循环中实现多处理/多重处理以使其运行更快?我应该定位哪个循环?

1 个答案:

答案 0 :(得分:0)

在第3个过程中打破第一个循环,

    def func(start, final):
         for x in xrange(start, final):
              for y in xrange(0,3000):
                   for z in xrange(0,700):
        operationg_tag = "%s-%s-%s" % (x,y,z)
        operation_code = ....
    def main():
        total = 14000
        div_total = int(14000 / 3)
        rest_div_total = 14000%3
        t1 = multiprocessing.Process(target = func,name = "", args=(100, div_total)
        t2 = multiprocessing.Process(target = func,name = "", args=(div_total, div_total*2)
        t3 = multiprocessing.Process(target = func,name = "", args=(div_total*2, div_total*3 + rest_div_total + 1)
        list_threads = [t1,t2,t3]
        for i in list_threads:
           i.start()
        for i in list_threads:
           i.join()

    if __name__ == "__main__":
        main()

如果你愿意,你可以创建更多的流程,但是这很好。