简体   繁体   English

Python多处理挂起

[英]Python multiprocessing hangs

I am starting to learn multiprocessing in python, but have arrived to a point where my code just hangs. 我开始学习python中的多处理,但是已经到了我的代码挂起的地步。 It is simply computing 1 000 000 factorial, using multithreading. 它仅使用多线程就可以计算出1 000 000阶乘。

import multiprocessing

def part(n):
    ret = 1
    n_max = n + 9999
    while n <= n_max:
        ret *= n
        n += 1
    print "Part "+ str(n-1) + " complete"
    return ret

def buildlist(n_max):
    n = 1
    L = []
    while n <= n_max:
        L.append(n)
        n += 10000
    return L

final = 1
ne = 0
if __name__ == '__main__':
    pool = multiprocessing.Pool()
    results = [pool.apply_async(part, (x,)) for x in buildlist(1000000)]
    for r in results:
        x = r.get()
        final *= x
        ne+= 1
        print ne
    print final

I have included some print functions to try to diagnose where the code hangs, and it will print the string included in the part function 100 times, as expected. 我提供了一些打印功能来尝试诊断代码的挂起位置,并且它将按预期将部分功能中包含的字符串打印100次。 The "print ne" also works 100 times. “打印ne”也可以工作100次。

The problem is that final won't print, and the code doesn't complete. 问题是最终版本将无法打印,并且代码无法完成。

How do I fix this problem? 我该如何解决这个问题?

Edit: Also, since this is being downvoted, could someone explain what I am doing wrong/why I am being downvoted? 编辑:此外,由于这被否决,有人可以解释我做错了/为什么被否决?

The program works fine --- until the print final . 程序运行正常---直到print final Then it spends a very large amount of time trying to print this number, which is seriously huge... 然后,它花费大量时间尝试打印此数字,这是非常大的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM