使用多处理模块并行化任务

import multiprocessing

def fib(n):
    """computing the Fibonacci in an inefficient way
    was chosen to slow down the CPU."""
    if n <= 2:
        return 1
    else:
        return fib(n-1)+fib(n-2) 
p = multiprocessing.Pool() 
print(p.map(fib,[38,37,36,35,34,33]))

# Out: [39088169, 24157817, 14930352, 9227465, 5702887, 3524578]

由于对 fib 的每次调用的执行并行发生,因此完整示例的执行时间比在双处理器上以顺序方式执行的速度快 1.8 倍

Python 2.2+