What is the difference between multithreading and multiprocessing in Python?
1.1 the thread
1.1.1 What is a thread
A thread is the smallest unit in which an operating system can schedule operations. It is contained within the process and is the actual operating unit within the process. A thread is a single sequential flow of control in a process, and multiple threads can be concurrent in a process, each performing a different task in parallel. A thread is an execution context, a list of instructions that a CPU needs to execute.
1.1.2 How threads work
Let’s say you’re reading a book, you haven’t finished it, and you want to take a break, but you want to get back to where you were when you came back. One way to do this is to write down the number of pages, lines, and words, which is an execution context. If your roommate is on your break, use the same method to read this book. You and she just need to memorize these three numbers so that you can read the book together at alternate times.
Threads work in a similar way. The CPU gives you the illusion of being able to do multiple operations at the same time, when in reality it spends very little time on each operation, essentially doing one thing at a time. It can do this because it has an execution context for each operation. Just as you can share the same book with your friends, multitasking can share the same CPU.
1.2 process
An execution instance of a program is a process. Each process provides all the resources needed to execute the program. (Processes are essentially collections of resources.)
A process has a virtual address space, executable code, an interface to the operating system, a secure context (recording the users and permissions that started the process, and so on), a unique process ID, environment variables, priority classes, a minimum and maximum workspace (memory space), and at least one thread.
Each process starts with the first thread, the main thread. The main thread then creates other child threads.
The resources related to the process include: memory pages (e.g. Open Sockets) file descriptors (E. LLDB) Security credentials (E. LLDB). The user ID that started the process)Copy the code
1.3 Differences between Processes and threads
- 1. Threads in the same process share the same memory space, but the processes are independent of each other.
- 2. All threads in the same process share data (process communication), and data between processes is independent.
- 3. Changes to the main thread may affect the behavior of other threads, but changes made by the parent (except for deletion) do not affect other child processes.
- 4. A thread is a context execution instruction, while a process is a cluster of resources associated with an operation.
- 5. Threads of the same process can communicate with each other directly, but the communication between processes needs to be realized by intermediate proxy.
- 6. Creating a new thread is easy, but creating a new process requires a copy of the parent process.
- 7. A thread can operate on other threads of the same process, but a process can only operate on its children.
- 8. Threads start fast and processes start slow (but there is no comparison).
Want to understand in detail please refer to the website: www.cnblogs.com/whatisfanta… Or www.cnblogs.com/yssjun/p/11…
2. What are mutable data types in Python? What are immutable data types?
Python3 has six basic data types: Number(int, float, bool, complex),String, List, Tuple, Dictionary, and Set
Variable data types: list, DIC,set
Immutable data types: Number, String, Tuple
What are mutable and immutable data types?
1. Immutable data types in Python do not allow the value of a variable to change. If you change the value of a variable, you create a new object. For objects with the same value, there is only one object in memory, and an internal reference count keeps track of how many variables refer to the object.
2. Mutable data types in Python allow the value of a variable to change. Append, +=, etc., change the value of a variable without creating a new object, and the address of the object referenced by the variable does not change. However, for different objects with the same value, there will be different objects in memory, that is, each object has its own address, which is equivalent to saving multiple copies of the object with the same value in memory. There is no reference counting here, it is a real object.
Numbers, strings, lists, dictionaries, Booleans
Mutable: lists, dictionaries immutable: numbers, strings, booleans
Mutable data types: The interior of a data type can change without changing the ID
Immutable data types: The ID changes as the value changes
How do you implement the singleton design pattern in Python? Link above:Blog.csdn.net/weixin_4228…
What is the singleton pattern?
The program ensures that a class has only one instance, and the class can instantiate itself and provide this instance to the whole system. This class is called a singleton class, and the singleton pattern is an object creation pattern.
How is the singleton pattern implemented?
In fact, singleton pattern is a design pattern, is a concept. Understand the principle and definition of singleton pattern. This can be done using any development language. The following small white simple use of Python language to achieve.
The Python module is a typical singleton pattern in the Python language. Python modules generate a. Pyc file the first time they are executed. When the execution module is called a second time, the.pyc file is called directly without executing the code.
Python singleton approach 1: Use Python modules.
Python singleton pattern implementation approach 2: Using Python”new“.
Experience from Baidu
4. In Python, a single line of code swaps two numbers
# Define two numbers and assign them
x = 1 y = 2
# 1: Introduce third-party variables
z = 0 z = x x = y y = z
# 2: Do not introduce third party variables
x = x+y y = x-y x = x-y
# 3: Recommendation
x,y = y,x
print("x=%d,y=%d"%(x,y))
5. What is the difference between __new__ and __init__ in Python
The init method is the initializer, and the __new__ method is the real constructor.
1. The __new__ method returns an instance object by default for use by __init__ and instance methods.
Reprinted at: blog.csdn.net/four_infini…
New () is a new class method, that is, only classes that inherit from Object have it. Note that newer classes have been added since Python2.2. In Python2. x there are both newer and older classes (also known as classic classes, mainly for compatibility with older code), but in Python3. x the older classes have been completely abandoned. In Python3. x, the system inherits from object by default even if python3.x does not explicitly say to inherit from object.
In older classes, instance constructors only call __init__() (at least on the outside), so there are many occasions when __init__() is called a constructor, which is fine in older classes, but not entirely appropriate in newer ones.
In the new class, instance construction calls __new__() to return an instance, and then calls __init__() to initialize the returned instance. So __new__() is the real constructor, init() just does the initialization.