[Twisted-Python] Returning a response for an HTTP query that requires upstream IO.
![](https://secure.gravatar.com/avatar/bf006aac4f248c75a22c3446679235d6.jpg?s=120&d=mm&r=g)
I'd like to develop a server that maintains a TCP connection to an upstream server, and listens for HTTP requests. Upon receiving an HTTP request, the server will send a message upstream, wait for a response, and return an HTTP response to the client which made the request. My background is in writing blocking web services, so my natural instinct is to write a `get_result_for(query)` method that will take a `query` string and return a result from the upstream TCP connection. I'm not sure whether writing such a method is possible in Twisted, since it's basically a blocking method. I suspect I may have to use Continuation-Passing Style instead, that is: provide a callback to `get_result_for(query, callback)`. Even if that's the case, how do I return an HTTP response to the HTTP client once the query result is available?
![](https://secure.gravatar.com/avatar/1ab02b123e4645acb4a53fe974d1742b.jpg?s=120&d=mm&r=g)
This section of the documentation might be what you're looking for: https://twistedmatrix.com/documents/current/web/howto/web-in-60/asynchronous... If the tcp request to your upstream server is using some blocking (non-twisted) API, you could look into deferToThread. On Wed, 22 Apr 2020 at 17:17, Go Luhng <goluhng@gmail.com> wrote:
I'd like to develop a server that maintains a TCP connection to an upstream server, and listens for HTTP requests. Upon receiving an HTTP request, the server will send a message upstream, wait for a response, and return an HTTP response to the client which made the request.
My background is in writing blocking web services, so my natural instinct is to write a `get_result_for(query)` method that will take a `query` string and return a result from the upstream TCP connection.
I'm not sure whether writing such a method is possible in Twisted, since it's basically a blocking method. I suspect I may have to use Continuation-Passing Style instead, that is: provide a callback to `get_result_for(query, callback)`.
Even if that's the case, how do I return an HTTP response to the HTTP client once the query result is available?
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com https://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Donal McMullan
-
Go Luhng