File: pipeline.rst

package info (click to toggle)
python-statsd 4.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: python: 912; makefile: 127
file content (50 lines) | stat: -rw-r--r-- 1,395 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
.. _pipeline-chapter:

=========
Pipelines
=========

The :py:class:`Pipeline` class is a subclass of :py:class:`StatsClient` that
batches together several stats before sending. It implements the entire client
interface, plus a :py:meth:`send() <Pipeline.send()>` method.

:py:class:`Pipeline` objects should be created with
:py:meth:`StatsClient.pipeline()`:

.. code-block:: python

    client = StatsClient()

    pipe = client.pipeline()
    pipe.incr('foo')
    pipe.decr('bar')
    pipe.timing('baz', 520)
    pipe.send()

No stats will be sent until :py:meth:`send() <Pipeline.send()>` is called, at
which point they will be packed into as few UDP packets as possible.


As a Context Manager
====================

:py:class:`Pipeline` objects can also be used as context managers:

.. code-block:: python

    with StatsClient().pipeline() as pipe:
        pipe.incr('foo')
        pipe.decr('bar')

:py:meth:`Pipeline.send()` will be called automatically when the managed block
exits.


Thread Safety
=============

While :py:class:`StatsClient` instances are considered thread-safe (or at least
as thread-safe as the standard library's ``socket.send`` is),
:py:class:`Pipeline` instances **are not thread-safe**. Storing stats for later
creates at least two important race conditions in a multi-threaded environment.
You should create one :py:class:`Pipeline` per-thread, if necessary.