[toc]

introduce

  • Multithreading versus multiprocess

    A process is a dynamic execution of a program on a data set.

    Threads appear to reduce the consumption of context switching, improve the concurrency of the system, and break through the defect that a process can only do the same thing, so that intra-process concurrency is possible.

    Usually there are multiple threads in a single process, and the CPU runs on threads, but Python does not have multithreading because GUI locks cause a single process to have only one thread.

  • Parallelism and concurrency

Some hardware has single-core processing, but switching is introduced in order to run multiple programs simultaneously (such as latex and Markdown software) without wasting resources because they can only be executed sequentially. In other words, multiple processes switch frequently to create the illusion of running simultaneously, or concurrency.

Parallelism is when multiple threads run at the same time without switching.

Code implementation

  • Thread class to create

    import threading import time def countNum(n): Print ("running on number:%s" %n) time.sleep(3) if __name__ == '__main__': T1 = threading. Thread (target = countNum, args = (23)) t2 = # generate a Thread instance threading. Thread (target = countNum, args = (34)) t1. The start () Start () print("ending!" )Copy the code