pymssql/pymssql — pull request #135 by msabramo (2013-10-04 01:41:27+00:00)
Title: Make pymssql green

The goal here is to make pymssql "green" - to make it work well with cooperative multitasking systems like gevent.

With something like gevent, one could spawn multiple greenlets that issue long-running SQL queries in parallel. If you have a few queries that take a while, then issuing them in parallel is a big win over issuing them serially.

This adds a function: `pymssql.set_wait_callback` which takes as an argument a callable "`wait_callback`".

The configured `wait_callback` is called in between `dbsqlsend` and `dbsqlok` and it is passes the file descriptor of the socket that FreeTDS is waiting for a read from - the wait callback is a good place to do something like a `gevent.socket.wait_read(read_fileno)` because this is after we've sent the query to the server and before we've received the result. So in the case of gevent, the query will be sent to the server and then the wait callback can do a `wait_read` to yield control to others greenlets until results come back from the server. That's for gevent, but the same idea can be used with other types of similar systems, because the wait callback is generic.