I am developing a multi-process application and using cassandra, I have a single session opened at the begining of the server and i want to share the session to other processes.I just want to know is it possible in cassandra(python driver) . if not why ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Yes, It is possible and recommended to use one session
4 simple rules when using the DataStax drivers for Cassandra
When using one of the DataStax drivers for Cassandra, either if it’s C#, Python, or Java, there are 4 simple rules that should clear up the majority of questions and that will also make your code efficient:
- Use one Cluster instance per (physical) cluster (per application lifetime)
- Use at most one Session per keyspace, or use a single Session and explicitely specify the keyspace in your queries
- If you execute a statement more than once, consider using a PreparedStatement
- You can reduce the number of network roundtrips and also have atomic operations by using Batches
Source http://www.datastax.com/dev/blog/4-simple-rules-when-using-the-datastax-drivers-for-cassandra
回答2:
No, its not recommended.
Quoting the official datastax documentation:
Be sure to never share any Cluster, Session, or ResponseFuture objects across multiple processes. These objects should all be created after forking the process, not before.
Source: https://datastax.github.io/python-driver/performance.html#multiprocessing