34 threads

A process, there will be one or more threads. The process is the smallest unit of resource allocation, the thread is CPU. Each process, at least one thread.

Process disadvantages:

  1. Process can only do one thing at a time, if you want to do more than one thing at the same time, we must use multiple processes.
  2. In the process of the implementation process, if blocked, such as waiting for input, the whole process will be suspended, even if some of the work process does not depend on input, will clog.

Threads are lightweight processes in the creation, revocation and switched using a very small space and time overhead.


The relationship between processes and threads
The relationship between processes and threads

Processes and threads difference:

  1. Address space and other resources: inter-process independent of each other, between each thread consent process sharing, thread and other processes within a process is not visible.
  2. Communication: inter-process communication IPC, the threads can read and write directly process data segment (such as global variables) to only communication - need aid process synchronization and mutual exclusion means to ensure data consistency.
  3. Scheduling and switching: switching thread context switching much faster than the process above.
  4. In multithreaded operating system, the process is not an executable entity.

Features thread:
multi-threaded operating system, usually in a process include multiple threads, each thread is a basic unit of cpu utilization, the cost is minimal overhead entity.

  1. Light entities:
    the thread is basically an entity does not own system resources, only essential thing is to have some, to ensure that space resources to run.
    Thread entities, including programs, data and TCB. A thread is a dynamic concept, described by the dynamic characteristics of his thread control block TCB (Thread Control Block).
    TCB contains the following information:
    - Thread state
    - When the thread is not running, do not save site resources
    - a set of execution Battle
    - store local variables for each thread to store
    - visit the same process of storage and other resources
    - is used to indicate is performed by mass hunt program counter, preserve the local variables and return addresses a small number of parameters like the turntable a set of registers and stack.

  2. The basic unit of scheduling and dispatching of independence

  3. Resource sharing process
    threads various threads in the same process, you can share resources owned by the process.

  4. Can be executed concurrently
    across multiple threads in the same process can be executed concurrently, even in a process to allow all the county can execute concurrently; the same, different threads in the process can be executed concurrently, full utilization of treatment and and the ability to peripheral devices operating in parallel.

Select the Python threading module

Python provides several modules for multi-threaded programming, including thread, threadingand Queueso on. threadAnd threadingmodule allows programmers to create and manage threads. threadModule provides support for basic threads and locks, threadingprovides a higher level, more powerful county management functions. QueueModule allows users to create a queue data structure may be used for sharing data among multiple threads.
Avoid using threadmodules, because of the higher-level threadingmodule is more advanced and more complete support for threads, but the use of threadthe module in the property and may threadingconflict. Second, low-level threadsynchronization primitives module rarely (there is only one), but threadingis there are many; Furthermore, threadthe module when the main thread ends, all the threads will be forced to end, without warning there will not be a normal clear work, at least threadingmodule can ensure that important sub-thread exits before the exit process.
threadModule does not support daemon thread, when the main thread exits, all of the child threads, whether they work, will be forced out. The threadingsupport daemon thread, usually a daemon thread waits for the server requests the client, if the client did not request it waits there, if set to a thread as a daemon thread, it means that the thread is not important in the process of withdrawal time, without waiting for the thread to exit.

Small chestnuts:

import time
from threading import Thread
#多线程并发
def func(n):
    time.sleep(2)
    print(n)
for i in range(10):
    t = Thread(target=func,args=(i,))	#并发创建10个线程,可以看到运行结果上,time.sleep(2)从视觉上看不出任何效果
    t.start()

among them
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43265998/article/details/91470350