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的更多相关文章

  1. Rabbitmqpool

    import pika import threading import random import uuid import json # 框架模块 from django.conf import se ...

随机推荐

  1. webpack的构建流程

    一.运行流程 webpack 的运行流程是一个串行的过程,它的工作流程就是将各个插件串联起来 在运行过程中会广播事件,插件只需要监听它所关心的事件,就能加入到这条webpack机制中,去改变webpa ...

  2. python 匿名函数,内置函数

    一 :匿名函数 匿名就是没有名字 def func(x,y,z=1): return x+y+z 匿名 lambda x,y,z=1:x+y+z #与函数有相同的作用域,但是匿名意味着引用计数为0,使 ...

  3. 15- web安全测试与appscan Scrawlr的使用

    web应用安全性问题 认证与授权测试要点 认证与授权测试要点之授权 session与cookie之cookie测试点: session测试点: 上传文件漏洞 SQL注入 SQL注入原理 SQL注入检查 ...

  4. 什么是响应式web设计

    什么是响应式web设计 现在开发一个产品,基本上都会需要兼顾 PC端和 移动端. 一般有两种思路: 1.为每个终端做一个特定的版本,并给2级域名,根据终端环境调用不同的版本代码. 2.一个网站能够兼容 ...

  5. $.ajax()——超时设置

    getAjax: function (method, apiUrl, options, callback) { var xhr = $.ajax({ type: method, url: apiUrl ...

  6. OAuth2(未完待续)

    一.OAuth2是什么?OAuth2解决了什么问题 1.OAuth2是第三方授权协议,用于支撑认证和授权 2.OAuth2中的角色划分: 资源拥有者 客户端 资源服务器 授权服务器 二.OAuth2怎 ...

  7. Linux中编写Shell脚本

    目录 Shell Shell脚本的执行 Shell脚本编写规范 Shell 中的变量 变量的算术运算 双小括号 (())  数值运算命令的用法 let 运算命令的用法 expr 命令的用法 br 命令 ...

  8. 第三部分 IDEA创建并运行项目

    可以创建一个maven,几行代码就解决了导入依赖,但是我的电脑不知道哪里出现了问题,IDEA重装,jdk重装,maven重装,都无法解决问题,找了3天,还是没有解决问题.最后只能采用手动导入包方法.看 ...

  9. Docker+Nginx配置SSL

    参考阿里云文档教程 前提条件 购买服务器的服务商开启443端口和服务器已开启443端口(https的默认端口) nginx容器已经安装http_ssl_module模块(启用SSL功能) 下面的教程基 ...

  10. 基于ray的分布式机器学习(二)

    基本思路:基于parameter server + multiple workers模式.同步方式:parameter server负责网络参数的统一管理,每次迭代均将参数发送给每一个worker,多 ...