File: third_party.rst

package info (click to toggle)
python-cassandra-driver 3.29.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,144 kB
  • sloc: python: 51,532; ansic: 768; makefile: 136; sh: 13
file content (64 lines) | stat: -rw-r--r-- 1,855 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
========================
Third party integrations
========================


Celery
------

Here's how, in substance, CQLengine can be plugged to `Celery
<http://celery.readthedocs.org/>`_:

.. code-block:: python

    from celery import Celery
    from celery.signals import worker_process_init, beat_init
    from cassandra.cqlengine import connection
    from cassandra.cqlengine.connection import (
        cluster as cql_cluster, session as cql_session)

    def cassandra_init(**kwargs):
        """ Initialize a clean Cassandra connection. """
        if cql_cluster is not None:
            cql_cluster.shutdown()
        if cql_session is not None:
            cql_session.shutdown()
        connection.setup()

    # Initialize worker context for both standard and periodic tasks.
    worker_process_init.connect(cassandra_init)
    beat_init.connect(cassandra_init)

    app = Celery()


uWSGI
-----

This is the code required for proper connection handling of CQLengine for a
`uWSGI <https://uwsgi-docs.readthedocs.org>`_-run application:

.. code-block:: python

    from cassandra.cqlengine import connection
    from cassandra.cqlengine.connection import (
        cluster as cql_cluster, session as cql_session)

    try:
        from uwsgidecorators import postfork
    except ImportError:
        # We're not in a uWSGI context, no need to hook Cassandra session
        # initialization to the postfork event.
        pass
    else:
        @postfork
        def cassandra_init(**kwargs):
            """ Initialize a new Cassandra session in the context.

            Ensures that a new session is returned for every new request.
            """
            if cql_cluster is not None:
                cql_cluster.shutdown()
            if cql_session is not None:
                cql_session.shutdown()
            connection.setup()