Python Notes 03 (Multi-threading)

Once you open the command line, check the local IP

windows + r

Command line input: cmd + ipconfig

Then check the IPv4 address: 192.168.1*6.1

ipconfig

Two-function multi-process

from multiprocessing import Process
import os, time


def func(name):
    print('进程的ID:', os.getpid())
    print('父进程的ID:', os.getppid())
    print('当前进程的名称:', name)
    time.sleep(3)


if __name__ == '__main__':
    # a
    process_lst = []
    for i in range(10):
        p = Process(target=func, args=(f'进程的名称为:{i}',))  # 默认调用init
        # 启动进程
        p.start()
        process_lst.append(p)

    for p in process_lst:
        p.join()    # 阻塞主进程,等待P进程执行完后继续
    print('主进程结束!')

Three inheritance multi-process

p.join() waits for the child process to end before proceeding to the next step.

from multiprocessing import Process
import time, os


class MyProcess(Process):
    def __init__(self, name):  # 初始化方法, 创建MyProcess时被调用
        Process.__init__(self)   # 调用父类的初始化
        self.name = name

    # 继承式必须重写run方法
    def run(self):
        print('进程的ID:', os.getpid())
        print('父进程的ID:', os.getppid())
        print('当前进程的名称:', self.name)
        time.sleep(3)


if __name__ == '__main__':
    process_lst = []
    for i in range(10):
        p = MyProcess(f'进程的名称:{i}')
        p.start()
        process_lst.append(p)

    for p in process_lst:
        p.join()
    print('主进程执行完毕')

Four inter-process communication (using queue, queue)

from multiprocessing import Process, Queue
import os
import random


# 入队的进程
class WriteProcess(Process):
    def __init__(self, name, q):
        Process.__init__(self)
        self.name = name
        self.q = q

    def run(self):
        print(f'进程的名称:{self.name}, 进程的ID:{os.getpid()}')
        # 入队操作
        for i in range(1, 6):
            x = random.randint(1000, 6666)
            print(f'入队元素:{x}')
            self.q.put(x)
        print('入队执行结束')


# 出队的进程
class ReadProcess(Process):
    def __init__(self, name, q):
        Process.__init__(self)
        self.name = name
        self.q = q

    def run(self):
        print(f'进程的名称:{self.name}, 进程的ID:{os.getpid()}')
        # 入队操作
        for i in range(1, 6):
            print(f'出队元素---------->{self.q.get()}')  # get是队列的方法


if __name__ == '__main__':
    # 主程序
    queue = Queue()  #
    # 创建子进程的对象
    write_process = WriteProcess('writer', queue)
    read_process = ReadProcess('reader', queue)

    # 启动进程
    write_process.start()
    read_process.start()

Five pipes were used for communication (pipe) at that time

1 needs to be configured in the init of the process: self.p = pipe 

2 Use data sending: p.send (value)

3 Data reception uses: p.recv (value)

from multiprocessing import Process, Pipe
import os


class WriteProcess(Process):
    def __init__(self, name, pipe):
        Process.__init__(self)
        self.name = name
        self.p = pipe  # pipe 是方法的局部变量,而self.p是类的实例属性,可以在类的任意方法中使用,而PIPE只能在init中使用

    def run(self):
        print(f'进程的名称:{self.name}, 进程的ID:{os.getpid()}')
        for i in range(1, 9):
            # 调用管道发送数据的方法: send
            self.p.send(i)

        print(f'写入进程执行完毕')


class ReadProcess(Process):
    def __init__(self, name, pipe):
        Process.__init__(self)
        self.name = name
        self.p = pipe

    def run(self):
        print(f'进程的名称:{self.name}')
        for i in range(5):
            print(f'出队元素:{self.p.recv()}')  # recv()接收数据


if __name__ == '__main__':
    #
    p1, p2 = Pipe()  # p1, p2指管道的两点,每一端对应一个进程
    wb = WriteProcess('writer', p1)
    rp = ReadProcess('reader', p2)

    # 启动进程
    wb.start()
    rp.start()

Six process pool

1 p.apply (run, ('task' + str(i),)) # Synchronous execution

2 p.apply_async (run, ('task' + str(i),)) # Asynchronous execution

# 函数式多进程
from multiprocessing.pool import Pool
import os, time, random

def run(name):
    start = time.time()  # 开始时间
    print(f'任务名称:{name}, 进程的ID:{os.getpid()}')
    time.sleep(random.choice([1, 2, 3, 4, 5]))  # 注意choice 要有()
    end = time.time()
    print(f' ________________+ 任务名称:{name}, 进程的ID:{os.getpid()}, 耗时:{round(end - start, 2)}')

if __name__ == '__main__':
    p = Pool(5)  # 5个进程的进程池
    # 5个进程,10个任务
    for i in range(10):
        # p.apply_async(run, ('任务' + str(i),))   # 异步执行
        p.apply(run, ('任务' + str(i),))   # 同步执行
    p.close()
    p.join()
    print('主进程结束')

Seven multi-thread locks

1 lock = Lock()     # Lock

2 lock = BoundedSemaphore(3)    # Can start 3 processes at the same time

3 lock. acquire()    # Start requesting lock

4 lock.release ()     # Release the lock

from threading import Thread, Lock, BoundedSemaphore
import time


# 函数实现多线程
# lock = Lock()
lock = BoundedSemaphore(3)  # 可以同时启动3个进程


def fun(num):
    lock.acquire()
    print(f"第{num}个人正在安检")
    time.sleep(3)
    print(f'-------> 第{num}个人安检完成')
    lock.release()


if __name__ == '__main__':
    for i in range(10):
        t = Thread(target=fun, args=(i,))
        t.start()

Guess you like

Origin blog.csdn.net/March_A/article/details/133280780