File: examples.rst

package info (click to toggle)
python-aiostream 0.5.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 368 kB
  • sloc: python: 2,445; makefile: 5
file content (64 lines) | stat: -rw-r--r-- 1,271 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
.. _example section:

Examples
========

Demonstration
-------------

The following example demonstrates most of the streams capabilities:

.. literalinclude:: ../examples/demo.py


Simple computation
------------------

This simple example computes ``11² + 13²`` in 1.5 second:

.. literalinclude:: ../examples/simple.py


Preserve a generator
--------------------

This example shows how to preserve an async generator from being closed
by the iteration context:

.. literalinclude:: ../examples/preserve.py


Norm server
-----------

The next example runs a TCP server that computes the euclidean norm of vectors for its clients.

Run the server:

.. sourcecode:: console

    $ python3.6 norm_server.py
    Serving on ('127.0.0.1', 8888)

Test using a netcat client:

.. sourcecode:: console

    $ nc localhost 8888
    --------------------------------------
    Compute the Euclidean norm of a vector
    --------------------------------------
    [...]

Check the logs on the server side, and see how the computation is performed on the fly.

.. literalinclude:: ../examples/norm_server.py
   :lines: 17-


Extra operators
---------------

This example shows how extra operators can be created and combined with others:

.. literalinclude:: ../examples/extra.py