异步的简单线程cassandra包装器
aiocassandra的Python项目详细描述
info: | Simple threaded cassandra wrapper for asyncio |
---|
安装
pip install aiocassandra
用法
importasynciofromaiocassandraimportaiosessionfromcassandra.clusterimportClusterfromcassandra.queryimportSimpleStatement# connection is blocking callcluster=Cluster()# aiocassandra uses executor_threads to talk to cassndra driver# https://datastax.github.io/python-driver/api/cassandra/cluster.html?highlight=executor_threadssession=cluster.connect()asyncdefmain():# patches and adds `execute_future`, `execute_futures` and `prepare_future`# to `cassandra.cluster.Session`aiosession(session)# best way is to use cassandra prepared statements# https://cassandra-zone.com/prepared-statements/# https://datastax.github.io/python-driver/api/cassandra/cluster.html#cassandra.cluster.Session.prepare# try to create them once on application initquery=session.prepare('SELECT now() FROM system.local;')# if non-blocking prepared statements is really needed:query=awaitsession.prepare_future('SELECT now() FROM system.local;')print(awaitsession.execute_future(query))# pagination is also supportedquery='SELECT * FROM system.size_estimates;'statement=SimpleStatement(query,fetch_size=100)# don't miss *s* (execute_futureS)asyncwithsession.execute_futures(statement)aspaginator:asyncforrowinpaginator:print(row)loop=asyncio.get_event_loop()loop.run_until_complete(main())cluster.shutdown()loop.close()
python 3.5+是必需的