File: gevent.rst

package info (click to toggle)
python-gevent 0.12.2-7
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,828 kB
  • ctags: 2,809
  • sloc: python: 9,151; makefile: 91; ansic: 42
file content (138 lines) | stat: -rw-r--r-- 4,585 bytes parent folder | download
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Basic utilities (The gevent top level package)
==============================================

.. module:: gevent

The most common functions and classes are available in the :mod:`gevent` top level package.


Greenlet objects
----------------

:class:`Greenlet` is a light-weight cooperatively-scheduled execution unit. 

To start a new greenlet, pass the target function and its arguments to :class:`Greenlet` constructor and call :meth:`start`:

>>> g = Greenlet(myfunction, 'arg1', 'arg2', kwarg1=1)
>>> g.start()

or use classmethod :meth:`spawn` which is a shortcut that does the same:

>>> g = Greenlet.spawn(myfunction, 'arg1', 'arg2', kwarg1=1)

To subclass a :class:`Greenlet`, override its _run() method and call ``Greenlet.__init__(self)`` in __init__:

>>> class MyNoopGreenlet(Greenlet):
...
...     def __init__(self, seconds):
...         Greenlet.__init__(self)
...         self.seconds = seconds
...
...     def _run(self):
...         gevent.sleep(self.seconds)

It also a good idea to override __str__(): if _run() raises an exception, its string representation will be printed after the traceback it generated.

.. class:: Greenlet

.. attribute:: Greenlet.value

    Holds the value returned by the function if the greenlet has finished successfully. Otherwise ``None``.

.. autoattribute:: Greenlet.exception

.. automethod:: Greenlet.ready
.. automethod:: Greenlet.successful
.. automethod:: Greenlet.start
.. automethod:: Greenlet.start_later
.. automethod:: Greenlet.join
.. automethod:: Greenlet.get
.. automethod:: Greenlet.kill(exception=GreenletExit, block=False, timeout=None)
.. automethod:: Greenlet.link(receiver=None)
.. automethod:: Greenlet.link_value(receiver=None)
.. automethod:: Greenlet.link_exception(receiver=None)
.. automethod:: Greenlet.unlink


Being a greenlet__ subclass, :class:`Greenlet` also has ``switch()`` and ``throw()`` methods.
However, these should not be used at the application level. Prefer higher-level safe
classes, like :class:`Event <gevent.event.Event>` and :class:`Queue <gevent.queue.Queue>`, instead.

__ http://codespeak.net/py/0.9.2/greenlet.html

.. exception:: GreenletExit

    A special exception that kills the greenlet silently.
    
    When a greenlet raises :exc:`GreenletExit` or a subclass, the traceback is not
    printed and the greenlet is considered :meth:`successful <Greenlet.successful>`.
    The exception instance is available under :attr:`value <Greenlet.value>`
    property as if it was returned by the greenlet, not raised.

Spawn helpers
-------------

.. function:: spawn(function, *args, **kwargs)

    Create a new :class:`Greenlet` object and schedule it to run ``function(*args, **kwargs)``.
    This is an alias for :meth:`Greenlet.spawn`.

.. function:: spawn_later(seconds, function, *args, **kwargs)

    Create a new :class:`Greenlet` object and schedule it to run ``function(*args, **kwargs)``
    in the future loop iteration *seconds* later.
    This is an alias for :meth:`Greenlet.spawn_later`.

.. function:: spawn_raw(function, *args, **kwargs)

    Create a new :class:`greenlet` object and schedule it to run ``function(*args, **kwargs)``.
    As this returns a raw greenlet, it does not have all the useful methods that
    :class:`gevent.Greenlet` has and should only be used as an optimization.

.. function:: spawn_link(function, *args, **kwargs)
              spawn_link_value(function, *args, **kwargs)
              spawn_link_exception(function, *args, **kwargs)

    This are the shortcuts for::

        g = spawn(function, *args, **kwargs)
        g.link() # or g.link_value() or g.link_exception()

    As :meth:`Greenlet.link` without argument links to the current greenlet, a :class:`gevent.greenlet.LinkedExited`
    exception will be raised if the newly spawned greenlet exits. It is not meant as a way of inter-greenlet communication
    but more of a way to assert that a background greenlet is running at least as long as the current greenlet.

    See :meth:`Greenlet.link`, :meth:`Greenlet.link_value` and :meth:`Greenlet.link_exception` for details.


Useful general functions
------------------------

.. autofunction:: getcurrent

.. autofunction:: sleep

.. autofunction:: kill(greenlet, exception=GreenletExit)

.. autofunction:: killall(greenlets, exception=GreenletExit, block=False, timeout=None)

.. autofunction:: joinall

.. autofunction:: signal

.. autofunction:: fork

.. autofunction:: shutdown

.. autofunction:: reinit


Timeouts
--------

.. autoclass:: Timeout
    :members:
    :undoc-members:

.. autofunction:: with_timeout