tags:

views:

557

answers:

1

I have come across several guides and packages on implementing a python JSON RPC server, e.g.:

They all do a good job in the sense that the server/application implementation is very simple, you just return the python object as a result and the framework takes care of serializing it. However, this is not suitable for my needs mainly because I am looking forward to serializing possibly thousands of records from database and such a solution would require me to create a single python object containing all the records and return that as the result.

The ideal solution I am looking for would involve a framework that would provide the application a stream to write the response to and a JSON encoder that could encode an iterator (in this case a cursor from pyodbc) on the fly, something like this:

def process(self, request, response):
  // retrieve parameters from request.

  cursor = self.conn.cursor()
  cursor.execute(sql) // etc.

  // Dump the column descriptions and the results (an iterator)
  json.dump(response.getOut(), [cursor.description, cursor])

Can someone point me to a server framework that can provide me a stream to write to and a json serialization framework that can handle an iterable such as the pyodbc cursor and serialize it on the fly.

+1  A: 

if the typical JSON-RPC frameworks doesn't allow you to dump such huge data effectively, why not just use a HTTP server and return json data, that way you can stream and read streamed data, good thing is you may even gzip it for faster transfer, and you will be able to use many standard servers too e.g. apache .

Anurag Uniyal
Thanks for the suggestion, I will explore this option later. Right now, I won't get any immediate benefit of streaming json like this because of two reasons:- I haven't found a json library that would let me stream on the fly- Even if I am able to stream from python, the client side can't deserialize it without having the whole json string in memory, so it is not like it can start consuming it immediately.I will have to do further analysis on what would workout better.
haridsv
you can dump json objects one by one
Anurag Uniyal
I think the JSON spec requires a single container at the top level, so this won't work unless the JSON library that you are using supports this concept. E.g., out of the two R libraries I tried, one gives a parser that can give you the object as soon as it can be instantiated (addData() followed by getObject()), but both give a toJSON() that fails to parse such strings.
haridsv