title: Python: Asynchronous Cassandra Inserts
body: <p>The problem with the Cassandra Python driver is that the "future" objects returned add a callback via <em>side effect</em>. Meaning the "future" is not composable in the same sense that a <code>Future</code> from either Javascript or Scala is composable. I am wondering if there is a pattern that can be used to bring about transforming a non-composable future into a composable future (preferably without leak memory.)</p>
  
  <pre><code>   my_query_object.insert(1, 2, 3, 'Fred Flinstone')
       .insert(1, 2, 3, 'Barney Rubble')
       .insert(5000, 2, 3, 'George Jetson')
       .insert(5000, 2, 3, 'Jane his wife')
  </code></pre>
  
  <p>Looking at the <a href="http://datastax.github.io/python-driver/performance.html" rel="nofollow">performance</a> section of the Cassandra Python driver from Datastax, I see an example of how they're creating a constantly chainable series of insert queries. Namely a slightly more complex version of this pattern:</p>
  
  <pre><code>def insert_next(previous_result=sentinel):
      if previous_result is not sentinel:
          if isinstance(previous_result, BaseException):
              log.error("Error on insert: %r", previous_result)
  
      future = session.execute_async(query)
      # NOTE: this callback also handles errors
      future.add_callbacks(insert_next, insert_next)
  </code></pre>
  
  <p>which works great as a toy example. The minute one query is done, another equivalent query is executed again. This scheme allows them to achieve 7k writes/s while the version which does not attempt to "chain" callbacks caps out around 2k writes/s.</p>
  
  <p>I've been trying to get my head around creating some sort of mechanism which allows me to recapture that exact mechanism but to no avail. Anyone come up with something like it?</p>