Python3 advanced --- multithreading and locking

1. First understand a few concepts

(1) Concurrency: multiple tasks are performed at the same time, but only one task can be processed at the same time, and there is only one processor, which is simultaneous in a logical sense

(2) Parallel: Multiple tasks are performed at the same time, and they are executed at the same time. There are multiple processors, which are simultaneous in the physical sense

(3) Process: refers to an application program running in the system; once the program runs, it is a process; process-the smallest unit of resource allocation

(4) Thread: The basic unit that the system allocates processor time resources, or a unit execution flow that executes independently within a process. Thread-the smallest unit of program execution

(5) A process includes at least one thread

 

2. Thread creation and invocation

# -*-coding:utf-8 -*-

import threading
import time

start_time = time.time()


def foo(name):
    start = time.time()
    print("hello %s" % name)
    time.sleep(2)
    end = time.time()
    cost_time = end - start
    print(cost_time)


# 创建线程
# target参数指向要执行的函数名
# args参数指向要执行的函数的参数,元组或者列表都可以
threading1 = threading.Thread(target=foo, args=("小明",))
threading2 = threading.Thread(target=foo, args=("小红",))

# 启动线程
threading1.start()
threading2.start()

# 堵塞主线程:线程.join(),直到当前线程运行结束才会继续运行主线程
# threading1运行完才会继续执行主线程
threading1.join()
# threading1运行完才会继续执行主线程
threading2.join()

end_time = time.time()

print("total time:", end_time - start_time)

 

3. Daemon thread

(1) Set thread t1 as a daemon thread through t1.setDaemon(True)

(2) The daemon thread will end after the main thread ends and will not continue to execute

(3) The daemon thread needs to be set before the thread starts

# -*-coding:utf-8 -*-

"""
守护线程:主线程结束,子线程也跟着结束
"""

import threading
import time

def foo():
    while True:
        print("生产了一个数据")
        time.sleep(1)

t1 = threading.Thread(target=foo)
# 守护线程必须在启动之前设置
# 主线程结束,t1跟着结束
t1.setDaemon(True)
t1.start()

print("主线程结束。。。。。")

 

4. Synchro lock

(1) Create a synchronization lock: lock=threading.Lock()

(2) Synchronization lock must be locked and unlocked in pairs, otherwise problems will occur

(3) Lock: lock.acquire(), lock before important data operation

(4) Unlock: lock.release(), unlock after important data operation

 

5. Deadlock

Thread deadlock: A deadlock (waiting for each other) caused by multiple threads competing for resources. If there is no external force, these processes will not be able to move forward.

Processing method:

(1) Recursive lock: recursive locks for threads between different tasks

         lock_R = threading.RLock()

        # Recursive lock maintains a calculator internally

        # Every time you lock, the counter is +1

        # Every time you unlock, the calculator is -1

        # Counter must be >=0

(2) Synchronization lock: the thread between the same task uses synchronization lock

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_19982677/article/details/108287958