File: examples.rst

package info (click to toggle)
python-pykka 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 508 kB
  • sloc: python: 2,813; makefile: 113
file content (114 lines) | stat: -rw-r--r-- 4,515 bytes parent folder | download | duplicates (5)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
========
Examples
========

The ``examples/`` dir in `Pykka's Git repo
<https://github.com/jodal/pykka/>`_ includes some runnable examples of Pykka
usage.


Plain actor
===========

.. literalinclude:: ../examples/plain_actor.py

Output::

    [{'no': 'Norway', 'se': 'Sweden'}, {'a': 3, 'b': 4, 'c': 5}]


Actor with proxy
================

.. literalinclude:: ../examples/typed_actor.py

Output::

    MainThread: calling AnActor.proc() ...
    MainThread: calling AnActor.func() ...
    MainThread: printing result ... (blocking)
    AnActor-1: this was printed by AnActor.proc()
    MainThread: this was returned by AnActor.func() after a delay
    MainThread: reading AnActor.field ...
    MainThread: printing result ... (blocking)
    MainThread: this is the value of AnActor.field
    MainThread: writing AnActor.field ...
    MainThread: printing new field value ... (blocking)
    MainThread: new value
    MainThread: calling AnActor.proc() ...
    MainThread: calling AnActor.func() ...
    MainThread: printing result ... (blocking)
    AnActor-1: this was printed by AnActor.proc()
    MainThread: this was returned by AnActor.func() after a delay
    MainThread: reading AnActor.field ...
    MainThread: printing result ... (blocking)
    MainThread: new value
    MainThread: writing AnActor.field ...
    MainThread: printing new field value ... (blocking)
    MainThread: new value
    MainThread: calling AnActor.proc() ...
    MainThread: calling AnActor.func() ...
    AnActor-1: this was printed by AnActor.proc()
    MainThread: printing result ... (blocking)
    MainThread: this was returned by AnActor.func() after a delay
    MainThread: reading AnActor.field ...
    MainThread: printing result ... (blocking)
    MainThread: new value
    MainThread: writing AnActor.field ...
    MainThread: printing new field value ... (blocking)
    MainThread: new value


Multiple cooperating actors
===========================

.. literalinclude:: ../examples/counter.py

Output::

    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 0
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 1 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 1
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 2 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 2
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 3 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 3
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 4 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 4
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 5 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 5
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 6 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 6
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 7 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 7
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 8 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 8
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 9 back
    Adder (urn:uuid:f50029eb-7cea-4ab9-98bf-a5bf65af8b8f) is increasing 9
    Bookkeeper (urn:uuid:4f2d4e78-7a33-4c4f-86ac-7c415a7205f4) got 10 back


Pool of actors sharing work
===========================

.. literalinclude:: ../examples/resolver.py


Mopidy music server
===================

Pykka was originally created back in 2011 as a formalization of concurrency
patterns that emerged in the `Mopidy music server <https://www.mopidy.com/>`_.
The original Pykka source code wasn't extracted from Mopidy, but it built and
improved on the concepts from Mopidy. Mopidy was later ported to build on Pykka
instead of its own concurrency abstractions.

Mopidy still use Pykka extensively to keep independent parts, like the MPD and
HTTP frontend servers or the Spotify and Google Music integrations, running
independently. Every one of Mopidy's more than 100 extensions has at least one
Pykka actor. By running each extension as an independent actor, errors and
bugs in one extension is attempted isolated, to reduce the effect on the rest
of the system.

You can browse the `Mopidy source code <https://github.com/mopidy/mopidy>`_ to
find many real life examples of Pykka usage.