首页 > 解决方案 > 为什么 Python 多处理不能在我的 PC 上运行?

问题描述

因此,由于我有一个 16 核 CPU,我想用这个脚本尝试多处理 Python 代码:

import time

NUMBERS = 50000, 50001, 50002, 50003, 50004, 50005, 50006, 50007, 50008, 50009, 50010, 50011, 50012, 50013, 50014, 50015

def factorial(n):
    print(f"start: factorial {n}")
    f = 1 
    for i in range(1, n+1):
        f*=i
    print(f"done: factorial {n}")
    return f


import multiprocessing as mp

t0 = time.time()
with mp.Pool() as pool: 
    result = pool.map(factorial, NUMBERS)
t1 = time.time()
print(f"Excecution took {t1-t0:.4f}")

但是当我在 Anaconda 的 Jupyter 发行版中执行单元格时,它不会输出任何内容,也不会停止。

平台信息:

CPU:i9 9900K

操作系统:Windows 10 专业版(10.0 Build 18362)

蟒蛇版本:1.9.7

Python 3.7.4

Jupyter 在其特殊目录中的 Windows Powershell 中使用以下命令单独启动

Jupyter notebook

标签: pythonpython-3.xjupyter-notebookanacondapython-multiprocessing

解决方案


推荐阅读