RabbitMaClientPoll
import pika
import threading
import random
import uuid
import json # 框架模块
from django.conf import settings """
Class:
Parameters:
Connectionsize:int类型,Rabbitmqpool池连接的最大数
Channelsize:int类型,Rabbitmqpool池Channel的最大数
return:None
""" # 单例保证唯一
class Rabbitmqpool:
# 定义类属性
__instance = None
__lock = threading.Lock() def __init__(self, Connectionsize, Channelsize):
self.maxConnectionsize = Connectionsize
self.maxChannelsize = Channelsize
self.nowConnectionsize = 0
self.nowChannelsize = 0
self.connectpool = {}
self.channelpool = {}
self.certdic = {} # def __new__(cls, Connectionsize, Channelsize):
# if not cls.__instance:
# cls.__instance = object.__new__(cls)
# return cls.__instance """
function: 获取一个空闲Channel或者新建一个Channel
Parameters: return:
channel:channel
cname:连接名
""" def get_channel(self):
try:
self.__lock.acquire()
cname = ""
channel = None
# 在已存在键中查找空闲Channel
for connectionname in self.connectpool:
if len(self.channelpool[connectionname]) != 0:
# print("取出一个Channel -----------------", len(self.channelpool[connectionname]))
channel = self.channelpool[connectionname][-1]
cname = connectionname
self.channelpool[connectionname] = self.channelpool[connectionname][0:-1]
# print("取出一个Channel")
break
# 如果没有找到空闲Channel,canme为"",则新建一个Channel
if cname == "":
if self.nowChannelsize < self.maxChannelsize:
# 从连接池返回一个连接的名字
if len(self.connectpool) != 0:
cname = random.choice(list(self.connectpool))
# 根据名字拿到此连接,传入连接和Pool池创建Channel
CreateChannel(self.connectpool[cname], self)
# 得到一个新Channel
channel = self.channelpool[cname][-1]
self.channelpool[cname] = self.channelpool[cname][0:-1]
print("创建一个Channel")
# 如果没有连接,则新建连接与channel
else:
if len(self.certdic) != 0:
cert = random.choice(list(self.certdic))
cname = str(uuid.uuid4().int)
print("创建一个连接")
CreateConnection(str(self.certdic[cert]["rabbitmq_host"]),
str(self.certdic[cert]["rabbitmq_port"]),
str(self.certdic[cert]["rabbitmq_virtual_host"]),
str(self.certdic[cert]["rabbitmq_user"]),
str(self.certdic[cert]["rabbitmq_password"]), self, cname,
self.certdic[cert]["heartbeat"])
CreateChannel(self.connectpool[cname], self)
# 得到一个新Channel
channel = self.channelpool[cname][-1]
self.channelpool[cname] = self.channelpool[cname][0:-1]
print("创建一个Channel")
else:
print("无法创建Channel,无连接凭证,不能创建连接!")
else:
print("无法创建Channel,超过限制") finally:
self.__lock.release()
return channel, cname def create_channel(self):
try:
self.__lock.acquire()
if len(self.certdic) != 0:
cert = random.choice(list(self.certdic))
cname = str(uuid.uuid4().int)
print("创建一个连接")
CreateConnection(str(self.certdic[cert]["rabbitmq_host"]), str(self.certdic[cert]["rabbitmq_port"]),
str(self.certdic[cert]["rabbitmq_virtual_host"]),
str(self.certdic[cert]["rabbitmq_user"]),
str(self.certdic[cert]["rabbitmq_password"]), self, cname,
self.certdic[cert]["heartbeat"])
CreateChannel(self.connectpool[cname], self)
# 得到一个新Channel
channel = self.channelpool[cname][-1]
self.channelpool[cname] = self.channelpool[cname][0:-1]
print("创建一个Channel")
return channel, cname
else:
print("无法创建Channel,无连接凭证,不能创建连接!")
return None, ""
finally:
self.__lock.release() def return_channel(self, channel, connectionname):
try:
self.__lock.acquire()
# print('还回去 return_channel')
self.channelpool[connectionname].append(channel)
# print('还回去 return_channel------------', len(self.channelpool[connectionname]))
finally:
self.__lock.release() def closepool(self):
pass def delconnection(self, connectionname):
try:
self.__lock.acquire()
if connectionname in self.connectpool:
print('删除链接connectionname', connectionname)
del self.connectpool[connectionname] self.nowConnectionsize = self.nowConnectionsize - 1
self.nowChannelsize = self.nowChannelsize - len(self.channelpool[connectionname])
print('删除connectionname', self.nowChannelsize)
del self.channelpool[connectionname] finally:
self.__lock.release() def get_certtemplate(self):
return {"rabbitmq_host": "", "rabbitmq_port": 5672, "rabbitmq_virtual_host": "", "rabbitmq_user": "",
"rabbitmq_password": "", "heartbeat": 6000} def addcert(self, cert):
self.certdic[cert["rabbitmq_host"]] = cert # 连接可以自己创建
class CreateConnection:
def __init__(self, rabbitmq_host, rabbitmq_port, rabbitmq_virtual_host, rabbitmq_user, rabbitmq_password,
Rabbitmqpool, Connectionname=str(uuid.uuid4().int), heartbeat=6000):
if Rabbitmqpool.nowConnectionsize < Rabbitmqpool.maxConnectionsize:
if Connectionname not in Rabbitmqpool.connectpool:
self.rabbitmq_user = str(rabbitmq_user)
self.rabbitmq_password = str(rabbitmq_password)
self.rabbitmq_host = rabbitmq_host
self.rabbitmq_port = rabbitmq_port
self.rabbitmq_virtual_host = rabbitmq_virtual_host
self.connectionname = Connectionname
self.heartbeat = heartbeat
# print(self.rabbitmq_user, self.rabbitmq_password, self.rabbitmq_host, self.rabbitmq_port,
# self.rabbitmq_virtual_host, self.connectionname)
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
try:
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(
host=rabbitmq_host,
port=rabbitmq_port,
virtual_host=rabbitmq_virtual_host,
heartbeat=heartbeat,
credentials=credentials))
Rabbitmqpool.connectpool[Connectionname] = self
Rabbitmqpool.nowConnectionsize += 1
if self.connectionname not in Rabbitmqpool.channelpool:
Rabbitmqpool.channelpool[self.connectionname] = []
print("创建连接:", Connectionname)
except Exception as e:
print("创建连接失败:", e)
else:
print("创建连接失败,此连接名已存在:", Connectionname)
else:
print("创建连接失败,连接池已满,无法创建连接池") def get_connection(self):
return self.connection class CreateChannel:
def __init__(self, Connection, Rabbitmqpool):
# print('创建 CreateChannel')
Rabbitmqpool.channelpool[Connection.connectionname].append(Connection.get_connection().channel())
Rabbitmqpool.nowChannelsize += 1 class RabbitMaClientPoll:
rabbitmq_host = settings.RABBIT_HOST
rabbitmq_port = 5672
rabbitmq_user = settings.RABBIT_USERNAME
rabbitmq_password = settings.RABBIT_PASSWORD
rabbitmq_virtual_host = "/"
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
Pool = Rabbitmqpool(100, 100)
cert = Pool.get_certtemplate()
cert['rabbitmq_host'] = rabbitmq_host
cert['rabbitmq_virtual_host'] = rabbitmq_virtual_host
cert['rabbitmq_user'] = rabbitmq_user
cert['rabbitmq_password'] = rabbitmq_password
cert['rabbitmq_port'] = rabbitmq_port
cert['heartbeat'] = 6000
Pool.addcert(cert)
instance = None def __init__(self):
cname_list = []
for i in range(100):
c, cname = self.Pool.create_channel()
cname_list.append((c, cname))
print('---', len(cname_list))
for item in cname_list:
c, cname = item
self.Pool.return_channel(c, cname) def __new__(cls, *args, **kwargs):
if cls.instance:
return cls.instance
else:
return super().__new__(cls) def producer(self, data):
""" :param data:
:return:
"""
data = json.dumps(data) try:
c, cname = self.Pool.get_channel()
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC,
body=data, )
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_ES,
body=data, ) self.Pool.return_channel(c, cname)
except Exception as e:
print("发送错误:", e) # 链接过期
self.Pool.delconnection(cname) # channel过期时,删除此链接和此链接下的所有channel
c, cname = self.Pool.create_channel() # 创建一个新的链接和channel
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC,
body=data, )
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_ES,
body=data, )
self.Pool.return_channel(c, cname) def producer_message(self, data):
""" :param data:
:return:
"""
data = json.dumps(data) try:
c, cname = self.Pool.get_channel()
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_MESSAGE,
body=data, ) self.Pool.return_channel(c, cname)
except Exception as e:
print("发送错误:", e) # 链接过期
self.Pool.delconnection(cname) # channel过期时,删除此链接和此链接下的所有channel
c, cname = self.Pool.create_channel() # 创建一个新的链接和channel
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_MESSAGE,
body=data, )
self.Pool.return_channel(c, cname) class RabbitMaClientPoll2(object):
rabbitmq_host = settings.RABBIT_HOST
rabbitmq_port = 5672
rabbitmq_user = settings.RABBIT_USERNAME
rabbitmq_password = settings.RABBIT_PASSWORD
rabbitmq_virtual_host = "/"
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
Pool = Rabbitmqpool(10, 5)
cert = Pool.get_certtemplate()
cert['rabbitmq_host'] = rabbitmq_host
cert['rabbitmq_virtual_host'] = rabbitmq_virtual_host
cert['rabbitmq_user'] = rabbitmq_user
cert['rabbitmq_password'] = rabbitmq_password
cert['rabbitmq_port'] = rabbitmq_port
cert['heartbeat'] = 3600 Pool.addcert(cert)
instance = None def __init__(self):
cname_list = []
for i in range(5):
print("===", len(cname_list))
print()
c, cname = self.Pool.create_channel()
cname_list.append((c, cname))
print("===", len(cname_list))
for item in cname_list:
c, cname = item
self.Pool.return_channel(c, cname)
pass def __new__(cls, *args, **kwargs):
if cls.instance:
return cls.instance
else:
return super().__new__(cls) def producer(self, data):
""" :param data:
:return:
"""
data = json.dumps(data) try:
c, cname = self.Pool.get_channel()
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC,
body=data, )
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_ES,
body=data, ) self.Pool.return_channel(c, cname)
except Exception as e:
print("发送错误:", e) # 链接过期
self.Pool.delconnection(cname) # channel过期时,删除此链接和此链接下的所有channel
c, cname = self.Pool.create_channel() # 创建一个新的链接和channel
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC,
body=data, )
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_ES,
body=data, )
self.Pool.return_channel(c, cname) def producer_message(self, data):
""" :param data:
:return:
"""
data = json.dumps(data) try:
c, cname = self.Pool.get_channel()
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_MESSAGE,
body=data, ) self.Pool.return_channel(c, cname)
except Exception as e:
print("发送错误:", e) # 链接过期
self.Pool.delconnection(cname) # channel过期时,删除此链接和此链接下的所有channel
c, cname = self.Pool.create_channel() # 创建一个新的链接和channel
c.basic_publish(exchange='',
routing_key=settings.QUEUE_TOPIC_MESSAGE,
body=data, )
self.Pool.return_channel(c, cname) RabbitMaClientPoll_obj_2 = RabbitMaClientPoll2()
RabbitMaClientPoll_obj = RabbitMaClientPoll()
RabbitMaClientPoll的更多相关文章
- Rabbitmqpool
import pika import threading import random import uuid import json # 框架模块 from django.conf import se ...
随机推荐
- Linux在shell终端中清空DNS缓存,刷新DNS的方法
现在很多Linux发行版都没有内置DNS本地缓存,Linux不像Windows那样可以使用ipconfig /flushdns来刷新,在Linux下无需刷新,因为本身没有缓存. 前言 在Linux系统 ...
- 从苏宁电器到卡巴斯基第13篇:我在苏宁电器当营业员 V
强大的竞争对手 与现在遍地开花的苹果店相比,在2010年左右的时候,在长春,真正得到苹果授权的苹果店还是屈指可数的.当时在重庆路上如果想买苹果的产品,要么可以去苏宁国美,要么只能去卓展楼上的苹果专区了 ...
- UVA11039
题意: 给你一个序列,由n个数字组成,每个数字的绝对值都不相同,然后让你从这n个数中拿出一些数,组成一个绝对值递增并且正负交替的最大序列,问组成的最大序列的最大长度是多少? 思路: ...
- MySQL UDF提权执行系统命令
目录 UDF UDF提权步骤 UDF提权复现(php环境) UDF UDF (user defined function),即用户自定义函数.是通过添加新函数,对MySQL的功能进行扩充,其实就像使用 ...
- Weblogic SSRF漏洞(CVE-2014-4210)
Weblogic中存在一个SSRF漏洞,利用该漏洞可以发送任意HTTP请求,进而攻击内网中redis.fastcgi等脆弱组件. 关于SSRF漏洞我们就不讲了,传送门--> SSRF(服务端请求 ...
- 0901-生成对抗网络GAN的原理简介
0901-生成对抗网络GAN的原理简介 目录 一.GAN 概述 二.GAN 的网络结构 三.通过一个举例具体化 GAN 四.GAN 的设计细节 pytorch完整教程目录:https://www.cn ...
- Python数模笔记-Sklearn(3)主成分分析
主成分分析(Principal Components Analysis,PCA)是一种数据降维技术,通过正交变换将一组相关性高的变量转换为较少的彼此独立.互不相关的变量,从而减少数据的维数. 1.数据 ...
- Jenkins 基础篇 - 任务分类
从前面的小节中我们看到在创建 Jenkins 任务的时候有好几种类型,如果你专门安装了 Maven 相关插件,可能还会有一个[构建一个 maven 项目]的任务类型,那这些任务类型究竟有何区别,以及我 ...
- xxl-job的一些感悟与规范
后台计划任务设计思路: 日志埋点处理,便于prd排查问题 2种主动job搭配规范(正向job.反查job) 1种消息接收的处理规范,重试机制,返回状态 job开关维度 数据流图 线上暗job-便捷性- ...
- Java安全之Filter权限绕过
Java安全之Filter权限绕过 0x00 前言 在一些需要挖掘一些无条件RCE中,大部分类似于一些系统大部分地方都做了权限控制的,而这时候想要利用权限绕过就显得格外重要.在此来学习一波权限绕过的思 ...