【转载】Python -- 多进程、多线程 的基本使用

https://www.cnblogs.com/jiyu-hlzy/p/15948408.html

 

单进程单线程

import time


def production():
    """
    间隔一秒,模拟一秒生产一个任务,生产10个任务
    :return: 生产完毕,返回需要消费的任务
    """
    _tasks = 0
    while _tasks < 10:
        time.sleep(1)
        _tasks += 1
        print(f"生产任务:{_tasks}")

    return _tasks


def consumption(_tasks, _count):
    """
    间隔一秒,模拟一秒消费完一个任务
    :param _tasks: 需要消费的任务数
    :param _count: 已经消费的任务数
    :return:
    """
    while _tasks:
        time.sleep(1)
        _tasks -= 1
        _count += 1
        print(f"完成任务:{_count} \t\t\t 剩余任务:{_tasks}")


if __name__ == '__main__':
    # 已经消费的任务数
    finishedTasks = 0

    # 生产任务
    numberOfTasks = production()
    # 消费任务
    consumption(numberOfTasks, finishedTasks)



单进程多线程

import time
from queue import Queue
from threading import Thread, current_thread


def production(_task_queue: Queue):
    """
    间隔一秒,模拟一秒生产一个任务
    一直生产任务
    :param: _task_queue
    """
    while True:
        time.sleep(1)
        _task_queue.put(1)
        print(f"生产任务:{_task_queue.qsize()} \t\t\t 线程名称:{current_thread().name}\n")


def consumption(_task_queue: Queue, _count_queue: Queue):
    """
    间隔一秒,模拟一秒消费完一个任务
    只要任务队列中有任务就一直消费
    :param _task_queue:  需要消费的任务数
    :param _count_queue: 已经消费的任务数
    """
    while True:
        if _task_queue.qsize():
            time.sleep(1)
            _task_queue.get()
            _count_queue.put(1)
            print(f"完成任务:{_count_queue.qsize()} \t\t\t 剩余任务:{_task_queue.qsize()} \t\t\t 线程名称:{current_thread().name}\n")
        else:
            # 队列中没任务的时候,间隔一段时间后,再继续
            print(f'所有任务消费完毕,休息一段时间,等待生产 \t\t 线程名称:{current_thread().name}\n')
            time.sleep(10)


if __name__ == '__main__':
    """
    线程间通信 用 queue.Queue 队列
    传参 args 传递的是元组
    只传一个参数时:args = (参数1,)
    """

    # 生产 队列
    tasksQueue = Queue()
    # 已经执行的任务数
    ftQueue = Queue()

    # 3个线程生产任务
    for i in range(1, 4):
        t = Thread(target=production, args=(tasksQueue,), name=f'生产线程{i}')
        t.start()

    # 5个线程消费任务
    for i in range(1, 6):
        t = Thread(target=consumption, args=(tasksQueue, ftQueue), name=f'消费线程{i}')
        t.start()



多进程单线程

import time
from multiprocessing import Process, current_process, Queue


def production(_task_queue: Queue):
    """
    间隔一秒,模拟一秒生产一个任务
    一直生产任务
    :param: _task_queue
    """
    while True:
        time.sleep(1)
        _task_queue.put(1)
        print(f"生产任务:{_task_queue.qsize()} \t\t\t 进程名称:{current_process().name}\n")


def consumption(_task_queue: Queue, _count_queue: Queue):
    """
    间隔一秒,模拟一秒消费完一个任务
    只要任务队列中有任务就一直消费
    :param _task_queue:  需要消费的任务数
    :param _count_queue: 已经消费的任务数
    """
    while True:
        if _task_queue.qsize():
            time.sleep(1)
            _task_queue.get()
            _count_queue.put(1)
            print(
                f"完成任务:{_count_queue.qsize()} \t\t\t 剩余任务:{_task_queue.qsize()} \t\t\t 进程名称:{current_process().name}\n")
        else:
            # 队列中没任务的时候,间隔一段时间后,再继续
            print(f'所有任务消费完毕,休息一段时间,等待生产 \t\t 进程名称:{current_process().name}\n')
            time.sleep(10)


if __name__ == '__main__':
    """
    进程间通信 用 multiprocessing.Queue 队列
    传参 args 传递的是元组
    只传一个参数时:args = (参数1,)
    """

    # 生产 队列
    tasksQueue = Queue()
    # 已经执行的任务数
    ftQueue = Queue()

    # 2个进程生产任务
    for i in range(1, 3):
        p = Process(target=production, args=(tasksQueue,), name=f'生产进程{i}')
        p.start()

    # 3个进程消费任务
    for i in range(1, 4):
        p = Process(target=consumption, args=(tasksQueue, ftQueue), name=f'消费进程{i}')
        p.start()



多进程多线程

import time
from threading import Thread, current_thread
from multiprocessing import Process, current_process, Queue


def production(_task_queue: Queue):
    """
    一个进程中,开多个线程进行生产
    :param _task_queue: 生产任务 队列
    """

    def production_individually():
        """
        间隔一秒,模拟一秒生产一个任务
        一直生产任务
        """
        while True:
            time.sleep(1)
            _task_queue.put(1)
            print(f"生产:{_task_queue.qsize()} \t 进程:{current_process().name} \t 线程:{current_thread().name}\n")

    # 每个进程中,3个线程生产
    for n in range(1, 4):
        t = Thread(target=production_individually, name=f'生产线程{n}')
        t.start()


def consumption(_task_queue: Queue, _count_queue: Queue):
    """
    一个进程中,开多个线程进行消费
    :param _task_queue:  需要消费的任务数
    :param _count_queue: 已经消费的任务数
    """

    def consumption_individually():
        """
        间隔一秒,模拟一秒消费完一个任务
        只要任务队列中有任务就一直消费
        """
        while True:
            if _task_queue.qsize():
                time.sleep(1)
                _task_queue.get()
                _count_queue.put(1)
                print(
                    f"完成:{_count_queue.qsize()} \t 剩余:{_task_queue.qsize()} \t "
                    f"进程:{current_process().name} \t 线程:{current_thread().name}\n"
                )
            else:
                # 队列中没任务的时候,间隔一段时间后,再继续
                print(f'所有任务消费完毕,休息一段时间,等待生产 \t 进程:{current_process().name} \t 线程:{current_thread().name}\n')
                time.sleep(10)

    # 每个进程中,2个线程消费
    for n in range(1, 3):
        t = Thread(target=consumption_individually, name=f'消费线程{n}')
        t.start()


if __name__ == '__main__':
    """
    进程间通信 用 multiprocessing.Queue 队列
    传参 args 传递的是元组
    只传一个参数时:args = (参数1,)
    """

    # 生产 队列
    tasksQueue = Queue()
    # 已经执行的任务数
    ftQueue = Queue()

    # 2个进程生产任务
    for i in range(1, 3):
        p = Process(target=production, args=(tasksQueue,), name=f'生产进程{i}')
        p.start()

    # 3个进程消费任务
    for i in range(1, 4):
        p = Process(target=consumption, args=(tasksQueue, ftQueue), name=f'消费进程{i}')
        p.start()
posted @ 2022-09-26 14:49  抬头微笑向前  阅读(26)  评论(0编辑  收藏  举报