There is an example of Spyne client?

2024/9/19 9:54:28

I'm trying to use spyne (http://spyne.io) in my server with ZeroMQ and MsgPack. I've followed the examples to program the server side, but i can't find any example that helps me to know how to program the client side.

I've found the class spyne.client.zeromq.ZeroMQClient , but I don't know what it's supposed to be the 'app' parameter of its constructor.

Thank you in advance!

Edit:

The (simplified) server-side code:

from spyne.application import Application
from spyne.protocol.msgpack import MessagePackRpc
from spyne.server.zeromq import ZeroMQServer
from spyne.service import ServiceBase
from spyne.decorator import srpc
from spyne.model.primitive import Unicodeclass RadianteRPC(ServiceBase):    @srpc(_returns=Unicode)def whoiam():return "Hello I am Seldon!"radiante_rpc = Application([RadianteRPC],tns="radiante.rpc",in_protocol=MessagePackRpc(validator="soft"),out_protocol=MessagePackRpc()
)s = ZeroMQServer(radiante_rpc, "tcp://127.0.0.1:5001")
s.serve_forever()
Answer

Spyne author here.

There are many issues with the Spyne's client transports.

First and most important being that they require server code to work. And that's because Spyne's wsdl parser is just halfway done, so there's no way to communicate the interface the server exposes to a client.

Once the Wsdl parser is done, Spyne's client transports will be revived as well. They're working just fine though, the tests pass, but they are (slightly) obsolete and, as you noticed, don't have proper docs.

Now back to your question: The app parameter to the client constructor is the same application instance that goes to the server constructor. So if you do this:

c = ZeroMQClient("tcp://127.0.0.1:5001", radiante_rpc)
print c.service.whoiam()

It will print "Hello I am Seldon!"

Here's the full code I just committed: https://github.com/arskom/spyne/tree/master/examples/zeromq

BUT:

All this said, you should not use ZeroMQ for RPC.

I looked at ZeroMQ for RPC purposes back when its hype was up at crazy levels, (I even got my name in ZeroMQ contributors list :)) I did not like what I saw, and I moved on.

Pasting my relevant news.yc comment from https://news.ycombinator.com/item?id=6089252 here:

In my experience, ZeroMQ is very fragile in RPC-like applications,especially because it tries to abstract away the "connection". Thismindset is very appropriate when you're doing multicast (and ZeroMQrocks when doing multicast), but for unicast, I actually want todetect a disconnection or a connection failure and handle itappropriately before my outgoing buffers are choked to death. So, I'devaluate other alternatives before settling on ZeroMQ as a transportfor internal RPC-type messaging.

If you are fine with having the whole message in memory before parsing(or sending) it (Http is not that bad when it comes to transferringhuge documents over the network), writing raw MessagePack document toa regular TCP stream (or tucking it inside a UDP datagram) will do thetrick just fine. MessagePack library does support parsing streams --see e.g. its Python example in its homepage (http://msgpack.org).

Disclosure: I'm just a happy MessagePack (and sometimes ZeroMQ) user.I work on Spyne (http://spyne.io) so I just have experience with someof the most popular protocols out there.

I seem to have written that comment more than a year ago. Fast forward to today, I got the MessagePack transport implemented and released in Spyne 2.11. So if you're looking for a lightweight transport for internally passing small messages, my recommendation would be to use it instead of ZeroMQ.

However, once you're outside the Http-land, you're back to dealing with sockets at the system-level, which may or may not be what you want, depending especially on the amount of resources you have to spare for this bit of your project.

Sadly, there is no documentation about it besides the examples I just put together here: https://github.com/arskom/spyne/tree/master/examples/msgpack_transport

The server code is fairly standard Spyne/Twisted code but the client is using system-level sockets to illustrate how it's supposed to work. I'd happily accept a pull request wrapping it to a proper Spyne client transport.

I hope this helps. Patches are welcome.

Best regards,

https://en.xdnf.cn/q/72221.html

Related Q&A

Safely bind method from one class to another class in Python [duplicate]

This question already has answers here:What is the difference between a function, an unbound method and a bound method?(6 answers)Closed 5 years ago.I know I can attach a function to a class and make …

Basic Python OpenCV cropping and resizing

can someone help me with a little cropping algorithm? its openCV.. im trying to figure this out. I know the method is crop = image[y:y1, x:x1]. If I have an image with new_dimensionXxnew_dimensionY pi…

Why does Keras loss drop dramatically after the first epoch?

Im training a U-Net CNN in Keras/Tensorflow and find that loss massively decreases between the last batch of the first epoch, and the first batch of the second epoch: Epoch 00001: loss improved from in…

extract strings from a binary file in python

I have a project where I am given a file and i need to extract the strings from the file. Basically think of the "strings" command in linux but im doing this in python. The next condition is …

Installing numpy on Mac to work on AWS Lambda

Is there a way to install numpy on a Mac so that it will work when uploaded to AWS Lambda? I have tried a variety of different ways, including using different pip versions, using easy_install, and fol…

python- how to get the output of the function used in Timer

I want to run a function for 10s then do other stuff. This is my code using Timerfrom threading import Timer import timedef timeout():b=truereturn ba=false t = Timer(10,timeout) t.start()while(a==f…

Create automated tests for interactive shell based on Pythons cmd module

I am building an interactive shell using Python 3 and the cmd module. I have already written simple unit tests using py.test to test the individual functions, such as the do_* functions. Id like to c…

Matplotlib with multiprocessing freeze computer

I have an issue with matplotlib and multiprocessing. I launch a first process, where I display an image and select an area, and close the figure. Then I launch another process, where I call a graph fun…

Pull Tag Value using BeautifulSoup

Can someone direct me as how to pull the value of a tag using BeautifulSoup? I read the documentation but had a hard time navigating through it. For example, if I had:<span title="Funstuff&qu…

What is the practical difference between xml, json, rss and atom when interfacing with Twitter?

Im new to web services and as an introduction Im playing around with the Twitter API using the Twisted framework in python. Ive read up on the different formats they offer, but its still not clear to m…