A modified rabbit-rpc of https://github.com/MidTin/rabbit-rpc
Project description
RPC based on rabbitmq, cloned from here
How To Use
The orignal code can just be runned in console and has problems in windows(Server side). So I modified some code. Now, the server side supports both pika.BlockConnection and pika.SelectionConnection. In BlockConnection mode, the server side can only be runned in single thread. In SelectionConnection mode, the server can be runned in multi-threads(Not in windows).
- Install:
pip install rabbitmq-rpc
-
Usage:
1.Create rabbitmq account in your rabbitmq server.
2.Run server.py first and then run client.py -
Server.py
from rabbitmq_rpc import server as Server
server = Server.RPCServer(queue_name='default',
amqp_url = "amqp://yourname:yourpasswd@10.147.17.135:5672/",
threaded=False)
@server.consumer()
def add(a, b):
return a+b
if __name__ == '__main__':
server.run()
- Client.py
from rabbitmq_rpc.client import RPCClient
import time
def add(i):
t1 = time.time()
client = RPCClient(amqp_url='amqp://yourname:yourpasswd@10.147.17.135:5672/')
res = client.call_add(0, i)
t2= time.time()
print(f"{0} + {i} = {res} RPC Time Cost: {t2-t1:.2f}")
return res
for i in range(300):
obj = add(i)
- Client with flask: client_with_flask.py
from rabbitmq_rpc.client import RPCClient
import flask
web = flask.Flask(__name__)
web.debug = True
import time
def add(i):
t1 = time.time()
client = RPCClient(amqp_url='amqp://yourname:yourpasswd@10.147.17.135:5672/')
res = client.call_add(0, i)
t2= time.time()
print(f"{0} + {i} = {res} RPC Time Cost: {t2-t1:.2f}")
return res
@web.route('/web/<n>')
def test_web(n):
res = add(int(n))
return str(res)
if __name__ == '__main__':
# Notice, In default, flask enabled threading.
# If single-thread is needed, pass in 'threaded=False' option
web.run(host = '0.0.0.0', use_reloader = False)
*Note: RPCClient is not thread-safe. This is because pika is not thread-safe. So, create a RPCClient object only in one thread. DO NOT use it in multi-threads. *
Original README
Note: replace rabbit_rpc with rabbitmq_rpc
==========
Rabbit RPC
==========
简述
----
这是对 RabbitMQ 的 Pika_ 库进行封装的,一套简易 RPC 客户端/服务端库。
安装说明
--------
::
pip install rabbit-rpc
使用事例
--------
服务端
~~~~~~
::
# project/consumers.py
from rabbit_rpc.consumer import consumer
@consumer(name='add')
def add(a, b):
return a + b
# project shell
rabbit_rpc worker --amqp 'amqp://guest:guest@localhost:5672/'
# with django
rabbit_rpc worker --amqp 'amqp://guest:guest@localhost:5672/' --django project
客户端
~~~~~~
::
from rabbit_rpc.client import RPCClient
client = RPCClient(amqp_url='amqp://guest:guest@localhost:5672/')
ret = client.call_add(1, 1, timeout=1)
# or ignore result
client.call_add(1, 1, ignore_result=True)
# specify routing_key
client.call_add(1, 1, routing_key='default')
.. _Pika: https://github.com/pika/pika
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file rabbitmq-rpc-0.1.7.tar.gz
.
File metadata
- Download URL: rabbitmq-rpc-0.1.7.tar.gz
- Upload date:
- Size: 15.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | da83e0efd909923c1fc8de80b01e4cc08dbe5ce45a5dcd2ffed95a30a75581b9 |
|
MD5 | e833a1b6f9e83e139ab91e9cb2f86c52 |
|
BLAKE2b-256 | 032f8f3450930ca84522b251100f185373a0b8cde230a5dca614b5aebf7c0133 |