创建线程和进程之间的时间差异

Posted

技术标签:

【中文标题】创建线程和进程之间的时间差异【英文标题】:Time differences between making threads and processes 【发布时间】:2020-11-04 08:14:26 【问题描述】:
import threading
from multiprocessing import Process
import time
import os
def MyTask():
    time.sleep(2)
t0 = time.time()
threads = []
for i in range(10):
    thread = threading.Thread(target=MyTask)
    thread.start()
    threads.append(thread)
t1 = time.time()
print("Total Time for Creating 10 Threads:  seconds".format(t1-t0))
for thread in threads:
    thread.join()

t2 = time.time()
procs = []
for i in range(10):
    process = Process(target=MyTask)
    process.start()
    procs.append(process)
t3 = time.time()
print("Total Time for Creating 10 Processes:  seconds".format(t3-t2))
for proc in procs:
    proc.join()

我应该比较创建线程和进程之间的时间。 但有一个问题。 这是一个结果。

Total Time for Creating 10 Threads: 0.0019941329956054688 seconds
Total Time for Creating 10 Processes: 0.06481027603149414 seconds
Total Time for Creating 10 Threads: 0.04188799858093262 seconds
Total Time for Creating 10 Threads: 0.01695418357849121 seconds
Total Time for Creating 10 Threads: 0.01595759391784668 seconds
Total Time for Creating 10 Threads: 0.003989696502685547 seconds
Total Time for Creating 10 Threads: 0.003989696502685547 seconds
Total Time for Creating 10 Threads: 0.001995563507080078 seconds
Total Time for Creating 10 Threads: 0.0019948482513427734 seconds
Total Time for Creating 10 Threads: 0.0019948482513427734 seconds
Total Time for Creating 10 Threads: 0.0009980201721191406 seconds
Total Time for Creating 10 Threads: 0.0009975433349609375 seconds

我只想要一个总时间来创建线程和进程。 我认为这是因为多处理。但是,我不知道如何解决它。

【问题讨论】:

我认为你应该打印after 加入所有线程,不是吗? 【参考方案1】:

也许你没看到

RuntimeError: 
    An attempt has been made to start a new process before the
    current process has finished its bootstrapping phase.

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable.

如果你使用 freeze_support() 它运行良好

import threading
from multiprocessing import Process, freeze_support
import time
import os


def MyTask():
    time.sleep(2)


def main():
    t0 = time.time()
    threads = []

    for i in range(10):
        thread = threading.Thread(target=MyTask)
        thread.start()
        threads.append(thread)

    t1 = time.time()
    print("Total Time for Creating 10 Threads:  seconds".format(t1-t0))

    for thread in threads:
        thread.join()

    t2 = time.time()
    procs = []

    for i in range(10):
        process = Process(target=MyTask)
        process.start()
        procs.append(process)

    t3 = time.time()
    for proc in procs:
        proc.join()
    print("Total Time for Creating 10 Processes:  seconds".format(t3-t2))


if __name__ == '__main__':
    freeze_support()
    main()

结果

Total Time for Creating 10 Threads: 0.0020024776458740234 seconds
Total Time for Creating 10 Processes: 0.10802268981933594 seconds

Process finished with exit code 0

【讨论】:

以上是关于创建线程和进程之间的时间差异的主要内容,如果未能解决你的问题,请参考以下文章

IPC共享内存和线程内存的性能差异

多线程和多进程的区别

进程和线程的区别(简要总结)

进程和线程的区别?创建线程的方法?

并发编程8 线程的创建&验证线程之间数据共享&守护线程&线程进程效率对比&锁(死锁/递归锁)

程序进程线程之间的区别