Python Advanced Skills Multithreading

Processes, Threads, and Parallel Execution

process, thread

Modern operating systems such as Mac OS X, UNIX, Linux, Windows, etc., all support "multitasking" operating systems.

  • Process: It is a program that runs on the system, so this program is called a running process, and a process ID is assigned to facilitate system management.
  • Thread: A thread belongs to a process. A process can open multiple threads to perform different tasks. It is the smallest unit of actual work of a process.

A process is like a company, it is the unit that the operating system runs and manages the program

Threads are like employees of a company. A process can have multiple threads (employees), which are the actual workers of the process.

  • Multiple processes can run in the operating system, that is, multitasking
  • Multiple threads can run in one process, that is, multi-threaded operation

important point:

Processes are memory isolated, that is, different processes have their own memory spaces. This is similar to different companies having different offices.

The memory is shared between threads, the thread belongs to the process, and the multiple threads in a process share the memory space owned by the process.

This is like sharing the office space of the company among the employees of the company.

parallel execution

Parallel execution means doing different jobs at the same time.

Processes are executed in parallel, and the operating system can run many programs at the same time, and these programs are all executed in parallel.

In addition to processes, threads can actually be executed in parallel.

That is, for example, a Python program can actually do:

  • A thread is outputting: hello
  • A thread is outputting: Hello

A program like this does two or more different things at the same time, we call it: multi-threaded parallel execution

Summarize:

1. What is a process

A program runs within the operating system, that is, it becomes a running process

2. What is a thread

There can be multiple threads inside the process, and the running of the program is essentially the actual work of the threads inside the process.

3. What is parallel execution

  • Multiple processes are running at the same time, that is, different programs are running at the same time, which is called: multi-task parallel execution
  • Multiple threads in a process are running at the same time, which is called: multi-threaded parallel execution

multithreaded programming

threading module

Most programming languages ​​allow multithreaded programming, and Python is no exception.

Python's multithreading can be implemented through the threading module.

 

Multi-threaded programming test

 

 Multi-threaded programming can be realized through the above code. Let a Python program start 2 threads and each thread executes a function

Parameter test

If you need to pass parameters, you can pass:

  • The args parameter is passed as a tuple (in order of parameters)
  • Or use the kwargs parameter to pass parameters in the form of a dictionary

code: 

"""
演示多线程编程的使用
"""
import time
# 多线程
import threading


def sing(msg):
    print(msg)
    time.sleep(1)


def dance():
    while True:     # 无限循环
        print("我在跳舞,啦啦啦")
        time.sleep(1)


if __name__ == '__main__':
    # 创建一个唱歌的线程 args=("我要唱歌 哈哈哈", )
    # target=sing  执行的目标任务名
    # args 以元组的方式给执行任务传参    元组是一个参数的话参数后面得加”,“
    # kwargs={"msg": "我在跳舞哦 啦啦啦"}   以字典的方式给执行任务传参
    sing_thread = threading.Thread(target=sing, args=("我要唱歌 哈哈哈", ))
    # sing_thread = threading.Thread(target=sing, kwargs=("我要唱歌 哈哈哈", ))
    # 创建一个跳舞的线程
    dance_thread = threading.Thread(target=dance)

    # 让线程去干活吧
    sing_thread.start()
    dance_thread.start()



结果:
我要唱歌 哈哈哈
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
我在跳舞,啦啦啦
...............

Guess you like

Origin blog.csdn.net/qq1226546902/article/details/132057568