Python - 串行进程、多线程、多进程都需要相同的时间在我的本地运行

标签 python multithreading multiprocessing

我正在调查this article并在我的本地进行相同的测试:

import os
import time
import threading
import multiprocessing

NUM_WORKERS = 4

def only_sleep():
    print "PID: %s, Process Name: %s, Thread Name: %s" % (os.getpid(), multiprocessing.current_process(), threading.current_thread())
    time.sleep(2)

def crunch_numbers():
    print "PID: %s, Process Name: %s, Thread Name: %s" % (os.getpid(), multiprocessing.current_process(), threading.current_thread())
    x = 0
    while x < 10000000:
        x += 1

def main():

    start_time = time.time()
    for _ in range(NUM_WORKERS):
        only_sleep()
    end_time = time.time()

    print "serial time", end_time - start_time

    start_time = time.time()
    threads = [threading.Thread(target=only_sleep()) for _ in range(NUM_WORKERS)]
    [thread.start() for thread in threads]
    [thread.join() for thread in threads]
    end_time = time.time()

    print "Threads time = ", end_time - start_time

    start_time = time.time()
    processes = [multiprocessing.Process(target=only_sleep()) for _ in range(NUM_WORKERS)]
    [process.start() for process in processes]
    [process.join() for process in processes]
    end_time = time.time()

    print "Process time = ", end_time - start_time

if __name__ == '__main__':
    main()

这是输出:

PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
serial time 8.01504993439
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
Threads time =  8.01574707031
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
PID: 60230, Process Name: <_MainProcess(MainProcess, started)>, Thread Name: <_MainThread(MainThread, started 140735648224064)>
Process time =  8.0206348896

以下是我的本地系统配置:

enter image description here

所有三个串行、多线程、多进程都花费相同的时间来运行。有人可以帮助我理解为什么会发生这种情况吗?

最佳答案

当你引用一个函数时,你不应该调用它:

threading.Thread(target=only_sleep()) 

应该是:

threading.Thread(target=only_sleep)

还有:

processes = [multiprocessing.Process(target=only_sleep()) for _ in range(NUM_WORKERS)]

应该是:

processes = [multiprocessing.Process(target=only_sleep) for _ in range(NUM_WORKERS)]

() 用于调用函数。

关于Python - 串行进程、多线程、多进程都需要相同的时间在我的本地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58578753/

相关文章:

python - 安装 yarp python 绑定(bind)时出现编译器错误

python - Pycharm 无法识别位于路径中的 Cython 模块

java - 调整多线程的公平性

python - 如何将消息记录到通用(更改)文件中

python - 如何使用 asyncio 和 multiprocess.map 获取数据

python - 使用 Python 的 Multiprocessing 模块执行同时和单独的 SEAWAT/MODFLOW 模型运行

python - 与 R 代码相比,需要正确使用 scipy.optimize.fmin_bfgs

python - 有没有办法获得 Tkinter 中相对于整个屏幕的小部件位置?

java - 多个消费者从 ConcurrentLinkedQueue 轮询相同的值两次

C# 套接字异步与多线程