Python FAQ

1. The difference between TCP and UDP

2. Python's garbage collection mechanism

Reference counting Principle: When an object's reference is created or copied, the object's reference count is incremented by 1; when an object's reference is destroyed, the object's reference count is decremented by 1, and when the object's reference count is reduced to 0, It means that the object is no longer used and its memory can be released.

Advantages: Reference counting has a great advantage, that is, real-time, any memory, once there is no reference to it, it will be immediately reclaimed, and other garbage collection techniques must be under certain special conditions to recover invalid memory .

Disadvantages: But it also has weaknesses. The additional operations of maintaining the reference count brought by the reference counting mechanism are directly proportional to the memory allocation and release performed by Python running, and the number of reference assignments is obviously higher than that of other garbage collection techniques. The additional operations brought about are only low in efficiency related to the amount of memory to be reclaimed. At the same time, there is another big problem with reference technology-circular references. Because objects refer to each other, the reference of each object will not be 0, so the memory occupied by these objects will never be released.

Mark-clear Mark-clear only focuses on objects that may cause circular references. Obviously, it is impossible for immutable objects such as PyIntObject and PyStringObject to generate circular references, because they cannot hold references to other objects. Circular references in Python always occur between container objects, that is, objects that can hold other objects internally, such as list, dict, class, and so on.

Principles: 1. Look for the collection of root objects as the starting point for garbage detection. The root objects are some global references and references in the function stack. The objects pointed to by these references cannot be deleted; 2. From root Starting from the object collection, following each reference in the root object collection, if an object can be reached, it means that the object is reachable and will not be deleted. This process is the garbage detection stage; 3. When the detection stage After the end, all objects are divided into reachable and unreachable parts, all reachable objects are reserved, and the memory occupied by other unreachable objects will be recycled. This is the garbage collection stage. (The bottom layer uses a linked list to connect the objects of these collections together)

Disadvantages: The marking and removal process is not efficient.

Generational collection Principle: All memory blocks in the system are divided into different collections according to their survival time. Each collection becomes a "generation". Python defines three generations of object collections by default. The frequency of garbage collection increases with the "generation". "The survival time increases and decreases. In other words, the longer the object is, the less likely it is to be garbage, and the frequency of garbage collection should be reduced. So how to measure this survival time: It is usually measured by several garbage collection actions. If an object has been garbage collected more times, it can be concluded: the longer the survival time of the object.

3. The difference between process/thread/coroutine

A process is a dynamic execution process of a program on a data set. The process consists of three parts: program, data set, and process control block. The program is used to describe which functions of the process and how to complete; the data set is the resource used in the execution of the program; the process control block is used to save the state of the program.

Multiple threads can be opened in one process, why should there be processes instead of threads? Because threads share a set of data in a program, if they are all made into processes and each process occupies a piece of memory, then this set of data will have to be copied to each program several times, which is unreasonable, so there are threads.

The relationship between process threads (1) A thread can only belong to one process, and a process can have multiple threads, but at least one thread is allocated to the process. The process is the main body of the program. All threads in the same process share this All resources of the process (3) CPU is allocated to the thread, that is, the thread that really runs on the CPU (4) The thread is the smallest unit of execution, and the process is the smallest unit of resource management

Coroutine, also known as micro thread, fiber. The coroutine seems to be a subroutine, but in the process of execution, it can be interrupted inside the subroutine, and then switch to execute other subroutines, and then return to continue execution when appropriate.

4. The difference between POST/GET

5. The difference between cookie/session

Recommended Posts

Python FAQ
Python multithreading
Python CookBook
Python3 dictionary
Python3 module
python (you-get)
Python string
Python basics
Python descriptor
Python basics 2
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Python basics 4
Python basics 5
Centos6 install Python2.7.13
Python answers questions
Python basic syntax (1)
Python exit loop
Ubuntu16 upgrade Python3
Centos7 install Python 3.6.
ubuntu18.04 install python2
Python classic algorithm
Relearn ubuntu --python3
Python2.7 [Installation Tutorial]
Python string manipulation
Python 3.9 is here!
Python study notes (1)
python learning route
CentOS7 upgrade python3
Python3 basic syntax
Python review one
linux+ubuntu solve python
Functions in python
Python learning-variable types
CentOS install Python 3.6
7 features of Python3.9
Python file operation
ubuntu12.04 install python3