【转载】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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值