Multi-tasking
Concurrency: executing multiple tasks alternately over a period of time
Parallelism: Colleagues perform multiple tasks together over a period of time
Process Process
Process: A program running on the system is called a running process, and a process ID is assigned to facilitate system management. The basic unit of the operating system for resource allocation and scheduling.
Single process vs multiple processes
+ / If the program takes advantage of the computer’s multi-core capabilities and lets the CPU handle some tasks at the same time, it is suitable for multi-process development.
Dig a hole first and learn later
Thread thread
Threads belong to processes. A process can start multiple threads to perform different tasks. It is the smallest unit of actual work of the process.
Multiple threads belonging to the same process share all resources owned by the process
Properties
Execution order
Out of order, a thread is decided to execute first through CPU scheduling
End sequence
The main thread will wait for all child threads to finish executing before ending.
Basic usage
Create thread object
thread_obj = thread.Thread(target=func)
Start thread execution
thread_obj.start()
import threading import time def thread1(): while True: print('thread 1') time.sleep(1) def thread2(): while True: print('thread 2') time.sleep(1) if __name__ == '__main__': # create thread t1 = threading.Thread(target=thread1) t2 = threading.Thread(target=thread2) # start thread t1.start() t2.start()
Wait for the current thread task to complete before continuing execution
thread_obj.join()
Execute tasks with parameters
args: Pass parameters in tuple mode *The order of parameters must be consistent
kwargs: Pass parameters to the task in the form of a dictionary *The dictionary key and parameter name must be consistent
Daemon thread
The main thread does not wait for the execution of the sub-thread to complete. After the main thread completes the execution, the sub-thread will automatically close.
me,thod1
threading.Thread(target = work, daemon = True)
method2
Thread object.setDaemon(True)
Get thread information
current_thread = threading.current_thread()
print(current_thread)
Set/get name
name = thread object.current_thread().getName()
Thread object.setName()
Custom thread
class Mythread(threading.Thread): def run(self): print('Execute this thread', self.args) t = Mythread(args = (100, )) t.start()
Thread safety
Multiple thread operations may cause data confusion
*Some data types (such as lists) are thread-safe
GIL lock
Global Interpreter Lock (Global Interpreter Lock) allows only one thread in a process to be called by the CPU at the same time
Lock synchronization lock
Create lock
lock_object = threading.RLock()
Lock
Thread object.acquire()
release lock
Thread object.release()
e.g.
import threading lock_object = threading.Lock() loop=1000 number = 0 def _add(count): lock_object.acquire() #Lock global number for i in range(count): number + = 1 print("t1") lock_object.release() #Unlock def _sub(count): lock_object.acquire() #Apply for lock global number for i in range(count): number -= 1 print("t2") lock_object.release() #Unlock t1 = threading.Thread(target=_add,args=(loop,)) t2 = threading.Thread(target=_sub,args=(loop,)) t1.start() t2.start() t1.join() t2.join() print(number)
RLock recursive lock
Lock does not support nesting of locks, but RLock does, that is, you can add multiple locks.
Dead Lock deadlock
Blockage due to resource competition or mutual communication
Thread pool
The more threads you open, the better. Opening too many threads may lead to reduced system performance (context switching in threads). Therefore, it is not recommended to create unlimited threads. It is recommended to use a thread pool.
Create
from concurrent.futures import ThreadPoolExecutor
pool = ThreadPoolExecutor(100) #Thread pool of 100 threads
Submit task
pool.submit(task, sug)
Waiting for the thread pool to complete
pool.shutdown(True)
Idle thread handles additional tasks
Submit a task to the thread pool. If there is an idle thread, allocate a thread to execute it. After the execution is completed, the thread is returned to the thread pool. If there is no idle thread, wait
future =pool.submit(task,url)
future.add_done_callback(done)
Single case mode
import threading import time class Singleton: instance = None lock = threading.RLock() def __init__(self,name): self.name = name def __new__(cls, *args, **kwargs): #*args, **kwargs represent any number of parameters # These two lines improve efficiency, optional or not if cls.instance: returncls.instance with cls.lock: if cls.instance: returncls.instance cls.instance = object.__new__(cls) returncls.instance def task(): obj = Singleton('x') print(obj) for i in range(10): t = threading.Thread(target=task) t.start()
Comparison between threads and processes
Relationship
Threads are attached to processes. Without processes, there would be no threads.
A process has one or more threads
Difference
The resource overhead of creating a process is greater than that of creating a thread.
Process is the basic unit of operating system resource allocation, and thread is the basic unit of CPU scheduling.
Threads cannot execute independently and must exist in the process
Advantages and Disadvantages
Process: multi-core possible, high overhead
Threads: not multi-core, low overhead
application:
Computationally intensive, multi-process. e.g Large amounts of data calculations
IO intensive, multi-threaded e.g file reading and writing, network data transmission
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeHomepageOverview 388649 people are learning the system