Python multithreading and coroutines

Table of contents

process and thread

serial and parallel

multithreaded programming

Thread class

Create thread parameters

Specific case 

Inherit the Thread class

Specific case

Thread Pool

Specific case

coroutine

The use of coroutines

Coroutine function writing

Call multiple coroutine functions

How to write the main function

the case

process and thread

Process: It is a program that runs on the system, then this program is called a running process, and an ID is assigned to facilitate system management

Thread: Threads belong to processes. A process can open multiple threads to perform different tasks. A thread is the smallest unit of actual work of a process.

Notice

  • The operating system can run multiple processes, that is, multi-tasking, and a process can have multiple threads, that is, multi-threading
  • There is at least one thread in a process, and there will be a main thread by default when starting a program
  • Processes are memory isolated, that is, different processes have their own memory space, which is similar to different companies having different offices
  • Memory is shared between threads, threads belong to a process, and multiple threads in a process share the memory space owned by the process.

serial and parallel

  • Serial: tasks are performed sequentially one after the other
  • Parallel: Multiple tasks are executed simultaneously (multiple cores of the CPU are required)

multithreaded programming

Thread class

#导入线程模块
import threading
#创建线程
thread_obj=threading.thread([group],target,[name],[args],[kwargs])
#使线程处于就绪状态
thread_obj.start()

Create thread parameters

  • group: temporarily useless, reserved parameters for future functions
  • target: the name of the target task to execute
  • args: pass parameters to the execution task in the form of tuples
  • kwargs: pass parameters to the executed task in the form of a dictionary
  • name: thread name

Specific case 

import threading
import time
def sing(msg):
    while True:
        print(msg)
        time.sleep(1)
def dance(email):
    while True:
        print(email)
        time.sleep(1)
if __name__ == '__main__':
    #因为target是第二个参数,所以必须给参数名进行传参
    sing_thread=threading.Thread(target=sing,args=("我在唱歌……",))
    dance_thread=threading.Thread(target=dance,kwargs={"email":"我在跳舞"})
    #启动线程
    sing_thread.start()
    dance_thread.start()

Inherit the Thread class

from threading import Thread

class MyThread(Thread):
    #重写run方法
    def run(self):
        线程内执行的代码
#创建线程
thread=MyThread()
#让线程处于就绪状态
thread.start()

Note: After the thread is executed, the rewritten run method is started 

Specific case

from threading import Thread
class MyThread(Thread):
    #重写run方法
    def run(self):
        for i in range(100):
            print(f"子线程:{i}")
if __name__ == '__main__':
    thread=MyThread()
    #开始调用线程
    thread.start()
    for i in range(100):
        print(f"主线程:{i}")

Thread Pool

Meaning: Open up some threads at one time, our users directly submit tasks to the thread pool, and the scheduling of thread tasks is handed over to the thread pool to complete

from concurrent.futures import ThreadPoolExecutor
#定义任务
def fn(任务参数):
    任务内执行的代码
#创建线程池,这里面的n表示创建n个线程的线程池
with ThreadPoolExecutor(n) as t:
    #提交任务
    t.submit(fn(任务参数))

Specific case

#导入线程池模块
from concurrent.futures import ThreadPoolExecutor
#定义任务
def fn(name):
    for i in range(100):
        print(name,i)
#main
if __name__ == '__main__':
    #创建线程池
    with ThreadPoolExecutor(50) as t:
        for i in range(100):
            #提交任务
            t.submit(fn("lili"))
    print("线程池的东西全部执行完毕!")

coroutine

Preface: When the program has IO operations, it can selectively switch to other tasks.

understand:

  • Macroscopically, what we can see is that multiple tasks are executed together
  • Microscopically, it is a task-by-task switching, and the switching condition is generally an IO operation

Note: Everything mentioned above is under the condition of single thread

The use of coroutines

Import the asyncio module: import asyncio

Coroutine function writing

async def 函数名():
    函数体

Note: The above function is an asynchronous coroutine function. After the function is executed, an asynchronous coroutine object will be obtained. If you want to call the function, you need to use the asyncio module

Coroutine object = coroutine function name ()

Call a single coroutine function: asyncio.run(coroutine object)

import asyncio
async def func():
    print("hello")
if __name__ == '__main__':
    g=func()
    asyncio.run(g)

Call multiple coroutine functions

Syntax: asyncio.run(main())

How to write the main function

#方法1
async def main():
    f1=协程函数1()
    await f1
    f2=协程函数2()
    await f2

#方法2
async def main():
    tasks=[
        #创建协程任务
        asyncio.create_task(fun1()),
        asyncio.create_task(fun2()),
        asyncio.create_task(fun3())
    ]
    await asyncio.wait(tasks)

the case

#导入协程模块
import asyncio
async def fun1():
    print("你好啊,我叫潘金莲")
    await asyncio.sleep(3)
    print("你好啊,我叫潘金莲")
async def fun2():
    print("你好啊,我叫周杰伦")
    await asyncio.sleep(2)
    print("你好啊,我叫周杰伦")
async def fun3():
    print("你好啊,我叫马保国")
    await asyncio.sleep(4)
    print("你好啊,我叫马宝国")
async def main():
    f1=fun1()
    await f1
    f2=fun2()
    await f2
    f3=fun3()
    await f3
if __name__ == '__main__':
    #一次性启动多个任务(异步协程方式)
    asyncio.run(main())

Note: When a synchronous operation (time.sleep(n)) occurs inside the function, the asynchrony will be interrupted. At this time, you need to use the sleep method of the asynchronous module. For the blocked method, add the await parameter in front to make the blocked function wait.

Guess you like

Origin blog.csdn.net/m0_60027772/article/details/132168654