python远程通信库
kiwip的Python项目详细描述
kiwipy
kiwipy是一个库,它使使用rabbitmq(以及为其编写后端的任何其他协议)进行远程消息传递变得容易。 我不知道你的情况,但我觉得拉比很难。 很容易犯一个配置错误,然后很难调试。 有了kiwipy,就没有这一切了,只是消息传递,变得简单,有了amqp的所有好的属性和保证。
您将得到以下信息:
- rpc
- 广播(带过滤器)
- 任务队列消息
让我们深入讨论一下,举几个来自rmq tutorial的例子。
rpc
客户:
fromkiwipyimportrmqcommunicator=rmq.RmqThreadCommunicator.connect(connection_params={'url':'amqp://localhost'})# Send an RPC messageprint(" [x] Requesting fib(30)")response=communicator.rpc_send('fib',30).result()print((" [.] Got %r"%response))
服务器:
importthreadingfromkiwipyimportrmqdeffib(comm,num):ifnum==0:return0ifnum==1:return1returnfib(comm,num-1)+fib(comm,num-2)communicator=rmq.RmqThreadCommunicator.connect(connection_params={'url':'amqp://localhost'})# Register an RPC subscriber with the name squarecommunicator.add_rpc_subscriber(fib,'fib')# Now wait indefinitely for fibonacci callsthreading.Event().wait()
工人
创建新任务:
importsysfromkiwipyimportrmqmessage=' '.join(sys.argv[1:])or"Hello World!"withrmq.RmqThreadCommunicator.connect(connection_params={'url':'amqp://localhost'})ascommunicator:communicator.task_send(message)
工人:
importtimeimportthreadingfromkiwipyimportrmqprint(' [*] Waiting for messages. To exit press CTRL+C')defcallback(_comm,task):print((" [x] Received %r"%task))time.sleep(task.count(b'.'))print(" [x] Done")try:withrmq.RmqThreadCommunicator.connect(connection_params={'url':'amqp://localhost'})ascommunicator:communicator.add_task_subscriber(callback)threading.Event().wait()exceptKeyboardInterrupt:pass
版本控制
这个软件遵循Semantic Versioning