程序要求:

1. 用Rabbit MQ实现RPC

1. 可以异步地执行多条命令

2. 可以对一次性对多个机器执行命令

程序效果:

---》run dir host1 host2 。。。。

---》get task_id

---》taskId:xxxx   host: xxxxxx

---》check task_id

--->打印结果

程序分析:

为了达到异步地效果,可以使用多线程或协程,即每执行一条命令就启动一条线程或协程。客户端发送命令到队列、从返回队列接收结果分离,不能写到一起。

业务逻辑:

代码实现:

README

#author:Wu zhiHao

#博客地址:https://www.cnblogs.com/BUPT-MrWu/p/10364619.html

#程序目录框架:
|--RPC
|--RPC_server #服务端
|--bin
|--start.py #程序入口
|--core
|--RpcServer.py #服务端主要逻辑
|--RPC_client #客户端
|--bin
|--start.py #程序入口
|--core
|--main.py #程序主要逻辑
|--modules
|--RpcClient.py #客户端主要逻辑
|--conf
|--settings.py #配置文件
|--READ_ME #命令格式:
1. run command host1 host2..... #执行命令
2. all_task #获取全部task_id
3. check task_id #获取命令结果

RPC_server\\bin\\start.py

import sys,os
BASE_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(BASE_dir)
from core import RpcServer
if __name__ == '__main__':
obj = RpcServer.RpcServer()
obj.channel.start_consuming()

RPC_server\\core\\RpcServer.py

import pika
import os
import socket
from conf import settings
class RpcServer(object):
def __init__(self):
self.credentials = pika.PlainCredentials(settings.RabbitMq_name,settings.RabbitMq_password) #RabbiMQ用户认证
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(
settings.RabbitMq_ip,settings.RabbitMq_port,"/",self.credentials,
)
)
self.My_Ip = self.get_ip() #获取服务端IP地址
self.channel = self.connection.channel()
self.result = self.channel.queue_declare(exclusive=True)
self.queue_name = self.result.method.queue
self.channel.exchange_declare(
exchange="Rpc",
exchange_type="direct",
)
self.channel.queue_bind(
exchange="Rpc",
queue=self.queue_name,
routing_key=self.My_Ip,
)
self.channel.basic_consume(
self.on_response,
queue=self.queue_name,
) def on_response(self,ch,method,properties,body):
command = body.decode()
command_result = self.on_request(command)
self.channel.basic_publish(
exchange="",
routing_key=properties.reply_to,
properties=pika.BasicProperties(
correlation_id=properties.correlation_id,
),
body=command_result
) def on_request(self,command):
return os.popen(command).read() def get_ip(self):
computer_name = socket.getfqdn(socket.gethostname( ))
computer_Ip = socket.gethostbyname(computer_name)
return computer_Ip

RPC_client\\bin\\start.py

import sys,os

BASE_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(BASE_dir)
from core import main
if __name__ == '__main__':
obj = main.run()
obj.start()

RPC_client\\core\\main.py

import random
import threading
from modules import RpcClient class run(object):
def __init__(self):
self.client = RpcClient.RpcClient()
self.information = {} def start(self):
while True:
try:
command = input("-->")
if not command:continue
t = threading.Thread(target=self.select,args=(command,))
t.start()
except Exception as e:
print(e) def select(self,command):
'''解析命令'''
try:
keyword = command.split()[0]
func = getattr(self,keyword)
func(command)
except Exception as e:
print(e) def run(self,command):
'''执行命令'''
try:
task_id = str(random.randint(100,1000))
self.information[task_id] = {}
keyword = command.split()[1]
for host in command.split()[2:]:
result = self.client.on_request(host,keyword)
self.information[task_id][host] = [result[0],result[1]]
except Exception as e:
print(e) def check(self,command):
'''获取命令结果'''
try:
task_id = command.split()[1]
for host in self.information[task_id]:
corr_id = self.information[task_id][host][0]
callback_queue = self.information[task_id][host][1]
command_result = self.client.get_response(corr_id,callback_queue)
print("%s:\n%s"%(host,command_result))
self.information.pop(task_id) #删除task_id
except Exception as e:
print(e) def all_task(self,command):
'''获取全部task_id'''
try:
for task_id in self.information:
all_host = []
for host in self.information[task_id]:
all_host.append(host)
print("task_id: %s host: %s\n"%(task_id,all_host))
except Exception as e:
print(e)

RPC_client\\conf\\settings.py

RabbitMq_name = "XXX" #RabbitMq用户名
RabbitMq_password = "XXX" #rabbitmq用户密码
RabbitMq_ip = "XXX" #RabbitMq端的IP地址
RabbitMq_port = 5672 #RabbitMq端的端口号

RPC_client\\mudules\\RpcClient.py

import pika
import uuid
from conf import settings
class RpcClient(object):
def __init__(self):
self.credentials = pika.PlainCredentials(settings.RabbitMq_name,settings.RabbitMq_password) #RabbiMQ用户认证
self.connection = pika.BlockingConnection(
pika.ConnectionParameters(
settings.RabbitMq_ip,settings.RabbitMq_port,"/",self.credentials,
)
)
self.channel = self.connection.channel() def get_response(self,corr_id,callback_queue):
'''从队列里取值'''
self.corr_id = corr_id
self.response = None
self.channel.basic_consume(
self.on_response,
queue=callback_queue,
)
while self.response is None:
self.connection.process_data_events() #非阻塞版的start_consuming
return self.response def on_response(self,ch,method,properties,body):
'''当队列里有数据时执行'''
if self.corr_id == properties.correlation_id:
self.response = body.decode() def on_request(self,host,command):
'''发送命令'''
result = self.channel.queue_declare(exclusive=False) #生成另一个queue时,这个queue不会消失
callback_queue = result.method.queue #返回queue
corr_id = str(uuid.uuid4()) #验证码
self.channel.exchange_declare(
exchange="Rpc",
exchange_type="direct"
)
self.channel.basic_publish(
exchange="Rpc",
routing_key=host,
properties=pika.BasicProperties(
correlation_id=corr_id,
reply_to=callback_queue,
),
body=command,
)
return corr_id,callback_queue #返回验证值和返回queue

程序执行实例:

python项目开发:用RabbitMQ实现异步RPC的更多相关文章

  1. python项目开发视频

    精品Python项目开发学习视频 所属网站分类: 资源下载 > python视频教程 作者:乐天派 链接:http://www.pythonheidong.com/blog/article/44 ...

  2. IDEA 学习笔记之 Python项目开发

    Python项目开发: 下载Python: https://www.python.org/downloads/release/python-363/ 安装Python: 配置环境变量(path): C ...

  3. python项目开发:学员管理系统

    学员管理系统 #需求: 1.用户角色:讲师/学员,登陆后根据角色不同能做的事情不同 2.讲师视图 - 管理班级,可创建班级,根据学员qq号把学员加入班级 - 可创建指定班级的上课纪录,注意一节上课纪录 ...

  4. python项目开发:ftp server开发

    程序要求: 1.用户加密认证 (对用户名密码进行MD5验证)2.允许同时多用户登陆 (使用socket server方法,为每个用户都创建一个信息文件)3.每个用户有自己的家目录,且只能访问自己的家目 ...

  5. Python项目开发公用方法--excel生成方法

    在实际开发中,我们有时会遇到数据导出的需求.一般的,导出的文件格式为Excel形式. 那么,excel的生成就适合抽离出一个独立的公用方法来实现: def generate_excel(excel_n ...

  6. Python基础之模块:7、项目开发流程和项目需求分析及软件开发目录

    一.项目开发流程 1.项目需求分析 明确项目具体功能: 明确到底要写什么东西,实现什么功能,在这个阶段的具体要询问项目经理和客户的需求 参与人员: 产品经理.架构师.开发经理 技术人员主要职责: 引导 ...

  7. 给缺少Python项目实战经验的人

    我们在学习过程中最容易犯的一个错误就是:看的多动手的少,特别是对于一些项目的开发学习就更少了! 没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用 ...

  8. 想使用gevent、mysql、sqlalchemy实现python项目协程异步达到并发的效果

    如题,但是查看了很多资料,都说python这边的mysql不支持异步并发,只能阻塞进行,心塞30秒,暂时放弃这方面的研究 如果不操作数据库的化,比如请求url.操作文件,还是可以用gevent来异步实 ...

  9. Python之路,Day22 - 网站用户访问质量分析监测分析项目开发

    Python之路,Day22 - 网站用户访问质量分析监测分析项目开发   做此项目前请先阅读 http://3060674.blog.51cto.com/3050674/1439129  项目实战之 ...

随机推荐

  1. 最新版本号cocos2d­2.0­x­2.0.2使用新资源载入策略!不再沿用-hd、-

     前段时间cocos2dx更新了最新版本号cocos2d­2.0­x­2.0.2.也从这个版本号開始对于资源载入与管理都改变了策略. 在之前的载入方式都是通过沿用与cocos2d-iphone一样 ...

  2. Oracle 堵塞(blocking blocked)

    堵塞是DBA常常碰到的情形,尤其是不良的应用程序设计的堵塞将导致性能严重下降直至数据库崩溃. 对DBA而言,有必要知道怎样定位到当前系统有哪些堵塞,究竟谁是堵塞者,谁是被堵塞者.本文对此给出了描写叙述 ...

  3. C++对象内存布局 (一)

    一.前言 在看了皓哥的C++对象的布局之后:http://blog.csdn.net/haoel/article/details/3081328.感觉自己还是总是不能记得很清楚,故在此总结一下C++对 ...

  4. Android重力感应器Sensor编程

    添加当重力变化时的处理函数在创建监听器时调用的函数 doSomething(x, y, z) 是自己定义的方法.当手机倾斜方向改变时,监听器会调用该方法.我们要做的,就是填充该方法,用于在重力发生变化 ...

  5. 虚拟机中的ip和本机的ip不是一个网段的

    将虚拟机的网络适配器 模式改为桥接模式 然后就会和主机处于同一个网段了

  6. Android定时任务

    前言 我们在平常的开发中可能会遇到一些需求,比如说,每日定时提醒,定时更新数据等等,反正就是周期性任务,碰到这类需求,我们就可以叫做定时任务.以前我们可以通过使用线程Handler来实现,现在既然是在 ...

  7. B1257 [CQOI2007]余数之和 数学,分块

    这个题想明白之后很好做,但是不好想.我根本没想出来,上网看了一下才知道怎么做... 这个题其实得数是一个等差数列,然后一点点求和就行了. 上次NOIP就是没看出来规律,这次又是,下次先打表找规律!!! ...

  8. B1051 受欢迎的牛 tarjan缩点

    就是一道tarjan缩点的板子,之前在洛谷做过.但是我发现一个事,就是函数里面有一句话: void tarjan(int x) { dfn[x] = low[x] = ++tot; str[++top ...

  9. treap平衡树

    今天集训讲平衡树,就瞎搞了一下.直接下代码. #include<iostream> #include<cstdio> #include<cmath> #includ ...

  10. class--类②

    定义 C++ 对象 类提供了对象的蓝图,所以基本上,对象是根据类来创建的.声明类的对象,就像声明基本类型的变量一样.下面的语句声明了类 Box 的两个对象: Box Box1; // 声明 Box1, ...