Skip to content
Snippets Groups Projects
Commit be3cee14 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added satella.cassandra, v2.8.6

parent 7f63e06d
No related branches found
No related tags found
No related merge requests found
...@@ -2,3 +2,4 @@ ...@@ -2,3 +2,4 @@
* added `time_us` * added `time_us`
* updated `stringify` to correctly handle None-cases * updated `stringify` to correctly handle None-cases
* added `satella.cassandra.parallel_for`
**This module is available only if you have cassandra-driver installed**
Cassandra
=========
parallel_for
------------
If you have multiple async requests that would hit multiple nodes
and you would prefer to make use of `execute_async` and want to better
make use of them, here's the routine to help you out.
.. autofunction:: satella.cassandra.parallel_for
...@@ -29,6 +29,7 @@ Visit the project's page at GitHub_! ...@@ -29,6 +29,7 @@ Visit the project's page at GitHub_!
time time
exceptions exceptions
processes processes
cassandra
Indices and tables Indices and tables
......
__version__ = '2.8.6_a3' __version__ = '2.8.6'
import typing as tp
from collections import namedtuple
def parallel_for(cursor, query: str, arguments: tp.Iterable[tuple]) -> tp.Iterator[namedtuple]:
"""
Syntactic sugar for
>>> futures = []
>>> for args in arguments:
>>> futures.append(cur.execute_async(query, args))
>>> for future in futures:
>>> yield future.result()
:param cursor: the Cassandra cursor to use
:param query: base query
:param arguments: iterable yielding arguments to use in execute_async
"""
futures = []
for args in arguments:
futures.append(cursor.execute_async(query, args))
for future in futures:
yield future.result()
...@@ -16,6 +16,7 @@ setup(keywords=['ha', 'high availability', 'scalable', 'scalability', 'server', ...@@ -16,6 +16,7 @@ setup(keywords=['ha', 'high availability', 'scalable', 'scalability', 'server',
extras_require={ extras_require={
'HTTPJSONSource': ['requests'], 'HTTPJSONSource': ['requests'],
'YAMLSource': ['pyyaml'], 'YAMLSource': ['pyyaml'],
'TOMLSource': ['toml'] 'TOMLSource': ['toml'],
'satella.cassandra': ['cassandra-driver']
} }
) )
from satella.cassandra import parallel_for
import unittest
class TestCassandra(unittest.TestCase):
def test_parallel_for(self):
class Cursor:
def __init__(self):
self.execute_times_called = 0
self.result_times_called = 0
def result(self):
self.result_times_called += 1
return []
def execute_async(self, query, args):
self.execute_times_called += 1
return self
cur = Cursor()
for row in parallel_for(cur, 'SELECT * FROM table', [(1,), (2, ), (3, )]):
pass
self.assertEqual(cur.execute_times_called, 3)
self.assertEqual(cur.result_times_called, 3)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment