使用多處理模組並行化任務

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+