python processes and threads(05)

python processes and threads(05)

1 python processes and threads

1.1 Process and thread concepts

Reference video: Dark Horse nanny-level video

进程:就是一个程序,运行在系统之上,那么便称之这个程序为一个
运行进程,并分配进程ID方便系统管理。
线程:线程是归属于进程的,一个进程可以开启多个线程,执行不同
的工作,是进程的实际工作最小单位。

进程就好比一家公司,是操作系统对程序进行运行管理的单位
线程就好比公司的员工,进程可以有多个线程(员工),是进程实际的工作者
操作系统中可以运行多个进程,即多任务运行
一个进程内可以运行多个线程,即多线程运行

​Notes :

进程之间是内存隔离的,即不同的进程拥有各自的内存空间。这就类似于不同的公司拥有不同的办公场所。
线程之间是内存共享的,线程是属于进程的,一个进程内的多个线程之间是共享这个进程所拥有的内存空间的。
这就好比,公司员工之间是共享公司的办公场所。

Insert image description here

1.2 Parallel execution

Parallel execution means doing different jobs at the same time.
Processes are executed in parallel. 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 also be executed in parallel.
That is, for example, a Python program can actually do:
. A thread is outputting: Hello
. A thread is outputting: Hello
. If a program like this does two or more different things at the same time, we call it: multi-threaded parallel execution.

1.3 Core usage syntax_threading module

绝大多数编程语言,都允许多线程编程,Pyhton也不例外。
Python的多线程可以通过threading模块来实现。

import threading

thread_obj=threading.Thread([group [, target [, name [, args [, kwargs]]]]])

    group: 暂时无用,未来功能的预留参数
    target: 执行的目标任务名
    args: 以元组的方式给执行任务传参
    kwargs: 以字典方式给执行任务传参
    name: 线程名,一般不用设置

#启动线程,让线程开始工作
thread_obj.start()

1.4 Multi-threaded programming

Insert image description here

1.4.1 Cases where single thread cannot satisfy the situation are as follows:
import time

def sing():
    while True:
        print("我在唱歌,啦啦啦...")
        time.sleep(1)

def dance():
    while True:
        print("我在跳舞,呱呱呱....")
        time.sleep(1)

if __name__ == '__main__':
    sing()
    dance()

Print:

Insert image description here

1.4.2 Multi-threading
1.4.2.1 Multi-threaded create statement
import  threading

sing_thread = threading.Thread( target=sing )  
dance_thread= threading.Thread( target=dance )  

sing_thread.start()  #启动线程1
dance_thread.start() #启动线程2
1.4.2.2 Case
import time
import  threading

def sing():
    while True:
        print("我在唱歌,啦啦啦...")
        time.sleep(1)

def dance():
    while True:
        print("我在跳舞,呱呱呱....")
        time.sleep(1)

if __name__ == '__main__':
	# 创建线程1,需要指定目标target,注意方法名不带括号
    sing_thread = threading.Thread( target=sing )  
     # 创建线程2,需要指定目标target,注意方法名不带括号
    dance_thread= threading.Thread( target=dance )  

    sing_thread.start()  #启动线程1
    dance_thread.start() #启动线程2
1.4.2 Use of multi-threaded parameter passing
1.4.2.1 Multi-threaded parameter passing using args and kwargs
thread_obj=threading.Thread([group [, target [, name [, args [, kwargs]]]]])

args: 以元组的方式给执行任务传参,注意参数是元组,如:args=("元素1",) ;元组点号,结束
kwargs: 以字典方式给执行任务传参,注意参数是字典类型,如: kwargs={"msg":"我在跳舞"} 
1.4.2.2 Use cases
import time
import  threading

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

def dance(msg):
    while True:
        print(msg)
        time.sleep(1)

if __name__ == '__main__':

    # 创建线程1,传参,注意:(args是元组类型,结尾有,)
    sing_thread = threading.Thread( target=sing, args=("我在唱歌",) )
    # 创建线程2,传参,注意:(kwargs是字典类型)
    dance_thread= threading.Thread( target=dance, kwargs={
    
    "msg":"我在跳舞"} )

    sing_thread.start()  #启动线程1 
    dance_thread.start() #启动线程2

Guess you like

Origin blog.csdn.net/weixin_42786460/article/details/133092003