Is it possible to somehow cancel xmlrpc client request?
Let say that in one thread I have code like:
svr = xmlrpclib.ServerProxy('http://localhost:9092')
svr.DoSomethingWhichNeedTime()
I don't mean some kind of TimeOut... Sometimes from another thread I can get event to cancel my work. And then I need to cancel this request.
I know that I can do it with twisted but, is it possible to do it with standard xmlrpclib?
First of all, it must be implemented on server side, not in client (xmlrpclib). If you simply interrupt your HTTP request to XML-RPC server, it's not guaranteed that long process running on the server will be interrupted at all. So xmlrpclib just can't have this functionality.
If you want to implement this behaviour, you need to create two type of requests. A request of first type will tell your server to start some long process. It must be executed in background (in another thread or process), and your XML-RPC server must send the response ("Process started!") to the client immediately. When you want to stop the process, client must send another request that will tell your server to stop executing of process.
Yes, if you want to do really dirty hacks....
Basically the ServerProxy object keeps a handle to the underlying socket/http connection. If you reached into those internals and simply close() the socket your client code will blow up with an exception. If you handle those properly its your cancel.
You can do it a little more sane if you register your own transport class for the ServerProxy via the transport parameter and give it some cancel method that does what you want.
That won't stop the server from processing things, unless it reacts to closing the channel directly.
Related
I open a single TCP connection to a gateway PC. My application will send messages to the gateway which will process the payload and pass on the message to another computer (based on payload) for processing.
For example, I send message A over the TCP connection which will be routed to computer A for response. But I may also need to send message B which goes to computer B.
Currently I simply use send(messageA) and then use recv() to wait for the response. The downside is that the recv() will block which means I can't send message B until something is received (and I can't do any other tasks).
I have read about the following options but am confused to the best for my use case.
Make the socket non-blocking. I send message A, call recv() (let's assume there is some delay in processing such that nothing is received immediately; so code moves on), move on to send message B and again call recv(). Now, it could be A or B that responds, which I can handle. But I need to call recv() again since only one response received so far; but what if computer A is down and never responds -- at some point I need to decide to stop calling recv(), right? On what basis would I do this?
Set a timeout on the socket. Again, send message A but assume computer A is down, so the code will wait for timeout before moving on which is wasted time.
Use select. Since I have only one socket and I don't think that helps here; plus, I understand select will block unless a timeout is set so no different, in this case, to the option above?
Use multithreading. Have one thread to process the main application and do the sending. And another thread that just calls recv() in an infinite loop (or a long timeout) that calls a callback whenever data is available. But then if the connection is closed from the main thread, will the recv thread cause an exception or hang?
I am really not sure what best practices are or the pitfalls of the options above. Which would be best option or is there another option?
(I'm using Python, in case it makes a difference).
I use python socket to send data to server, and the code like:
When I close the server, it will send the data twice, and then, it will go to the "except" code. If I set the SEND_INTERVAL too long, it will be a disaster. So, how to get the error immediately when the server is closed or downtime?
Nothing happens immediatly over the network. That's one thing.
Secondly the underlying OS will detect broken connections (and Python gets that info in the form of an exception), but usually this takes time. And that's why you still send messages even though the connection is already dead. But since OS operates on network layer (as opposed to the application layer) then there's an issue: the connection may be dead but OS may never detect this. For example this will happen when the server is dead but behind alive proxy.
Thirdly the most reliable way to know that a server is alive is when it sends something back to the client. So you should always .recv() (with timeout) after .sendall() call and the server should always .sendall() after .recv() (the request-response pattern, even when the response is a simple "I received message"). If you can't modify the server side and (in worst case) if the server doesn't send anything back to the client then there's no reliable way to know this.
That's why you need some form of framing/correctness protocol. Simple .sendall() won't do.
I am trying to incorporate a legacy SSE Server + SSE Client with tornado. (A server that collects SSEs from processes, and distributes them to clients through a UDP socket) The first SSE GET request that we make, works perfectly. The only issue is tornado gets locked up when the user navigates away from the web app, and back. The web application will never load a second time.
I have a RequestHandler that is NOT asynchronous, and uses the client to wait in a while True loop reading from a non blocking python UDP socket. These messages are then written, and flushed to the browser. The browser successfully receives the SSEs.
In my RequestHandler the on_connection_close, and on_finish are never called. These are supposed to stop the client, and break from the while True loop. Is this because my get request is NOT a coroutine?
What is the correct way to do this in Tornado? I can show a code snippet if it's really needed, but the question should be self explanatory.
I was able to figure it out myself, after some experimentation.
on_finish() was never called because I need to call finish(), and on_connection_close() was never called because it was not a coroutine. I was able to resolve my issue by using the keyword yield.
More information can be found here: http://www.tornadoweb.org/en/stable/guide/coroutines.html
I have Tornado websocket handler and I am sending messages from my browser ( I have override on_message,on_close,open).
In javascript on close I want to send some data to handler ( to clean some storages, I am sending some numbers in json like {'storage':22, 'time':96} ).
How in websocket handler in tornado to receive that closing message ?
I looked at close and on_close but there is no option to receive data.
If I understand what you're asking for correctly, it's impossible.
You want to make sure that when the connection is closed, and the browser calls the on_close function on your client-side JavaScript code, it can send some final data to the Tornado server.
But when the connection is closed, there's no way to send any more data. That's what it means to be closed.
What you need to do is create a "quit" or similar message, at the application level. When Tornado sends a "quit" message to the JS code, then it can send its final message; when Tornado receives that message, it can close the socket. (Of course this means you need to write your code to handle the case where that "graceful shutdown" never happens because, e.g., the client machine has been vaporized by a nuclear bomb.)
I have nginx in front of 8 instances of Tornado, and for some requests (a handler for comments), I need Tornado to push messages on ZeroMQ. I am doing this at the end of the handler (just before I send the response to the client):
# here is body of handler for comments
context = zmq.Context()
port = "5252"
socket = context.socket(zmq.PUSH)
socket.bind("tcp://*:%s" % port)
print "Running server on port: ", port
socket.send("Commented")
# here I flush response to client
But this is hanging. Is this real way to push to ZeroMQ whenever the handler is executed?
Is this real way to push to ZeroMQ whenever the handler is executed?
No. Your code calls zmq.Context() every time the request handler is invoked. This is bad. It should be called exactly once - usually at the very beginning of your process, perhaps in some kind of init handler. You can safely share the context instance among any number of threads.
Same thing with socket creation and binding - this should be done once at startup. You must be more careful with the socket. If all your handlers (application, request etc) are executing in the same thread each time the handler is called, then you can use the same socket.
Another problem is the way your a "send"-ing to a PUSH socket. As described in http://api.zeromq.org/3-2:zmq-socket, a send to a PUSH socket may very well block in certain situations and you probably want to avoid that. Use the zmq.Poller with the POLLOUT flag (and a 0 timeout) to determine if the send would block. If not, then send right away. If so, you have to decide if you want to just drop the message or store it in your application to try again later.