- :meth:`get <gevent.Greenlet.get>` -- returns the value returned by greenlet or re-raised the exception that killed it.
It is possible to customize the string printed after the traceback by subclassing the :class:`Greenlet` class
It is possible to customize the string printed after the traceback by subclassing the :class:`gevent.Greenlet` class
and redefining its ``__str__`` method.
To subclass a :class:`Greenlet`, override its :meth:`_run` method and call ``Greenlet.__init__(self)`` in ``__init__``::
To subclass a :class:`gevent.Greenlet`, override its
:meth:`gevent.Greenlet._run` method and call
``Greenlet.__init__(self)`` in ``__init__``::
class MyNoopGreenlet(Greenlet):
...
...
@@ -162,7 +188,7 @@ To subclass a :class:`Greenlet`, override its :meth:`_run` method and call ``Gre
return 'MyNoopGreenlet(%s)' % self.seconds
Greenlets can be killed asynchronously. Killing will resume the sleeping greenlet, but instead
of continuing execution, a :exc:`GreenletExit` will be raised.
of continuing execution, a :exc:`gevent.greenlet.GreenletExit` will be raised.
>>> g = MyNoopGreenlet(4)
>>> g.start()
...
...
@@ -170,11 +196,11 @@ of continuing execution, a :exc:`GreenletExit` will be raised.
>>> g.dead
True
The :exc:`GreenletExit` exception and its subclasses are handled differently than other exceptions.
Raising :exc:`GreenletExit` is not considered an exceptional situation, so the traceback is not printed.
The :exc:`GreenletExit` is returned by :meth:`get <Greenlet.get>` as if it were returned by the greenlet, not raised.
The :exc:`gevent.greenlet.GreenletExit` exception and its subclasses are handled differently than other exceptions.
Raising :exc:`gevent.greenlet.GreenletExit` is not considered an exceptional situation, so the traceback is not printed.
The :exc:`gevent.greenlet.GreenletExit` is returned by :meth:`get <gevent.Greenlet.get>` as if it were returned by the greenlet, not raised.
The :meth:`kill <Greenlet.kill>` method can accept a custom exception to be raised:
The :meth:`kill <gevent.Greenlet.kill>` method can accept a custom exception to be raised:
>>> g = MyNoopGreenlet.spawn(5) # spawn() creates a Greenlet and starts it
>>> g.kill(Exception("A time to kill"))
...
...
@@ -183,30 +209,46 @@ The :meth:`kill <Greenlet.kill>` method can accept a custom exception to be rais
Exception: A time to kill
MyNoopGreenlet(5) failed with Exception
The :meth:`kill <Greenlet.kill>` can also accept a *timeout* argument specifying the number of seconds to wait for the greenlet to exit.
Note, that :meth:`kill <Greenlet.kill>` cannot guarantee that the target greenlet will not ignore the exception, thus it's a good idea always to pass a timeout to :meth:`kill <Greenlet.kill>`.
The :meth:`kill <gevent.Greenlet.kill>` can also accept a *timeout*
argument specifying the number of seconds to wait for the greenlet to
exit. Note, that :meth:`kill <gevent.Greenlet.kill>` cannot guarantee
that the target greenlet will not ignore the exception (i.e., it might
catch it), thus it's a good idea always to pass a timeout to
:meth:`kill <gevent.Greenlet.kill>`.
.. tip:: The exact timing at which an exception is raised within a
target greenlet as the result of :meth:`kill
<gevent.Greenlet.kill>` is not defined. See that function's
documentation for more details.
Timeouts
--------
Many functions in the gevent API are synchronous, blocking the current greenlet until the operation is done. For example,
:meth:`kill <Greenlet.kill>` waits until the target greenlet is :attr:`dead` before returning [#f1]_. Many of those
functions can be made asynchronous by passing the argument ``block=False``.
Many functions in the gevent API are synchronous, blocking the current
greenlet until the operation is done. For example, :meth:`kill
<gevent.Greenlet.kill>` waits until the target greenlet is
:attr:`gevent.greenlet.Greenlet.dead` before returning [#f1]_. Many of
those functions can be made asynchronous by passing the argument
``block=False``.
Furthermore, many of the synchronous functions accept a *timeout* argument, which specifies a limit on how long the function
can block (examples: :meth:`Event.wait`, :meth:`Greenlet.join`, :meth:`Greenlet.kill`, :meth:`AsyncResult.get`, and many more).
Furthermore, many of the synchronous functions accept a *timeout*
argument, which specifies a limit on how long the function can block
The detailed information is available in changelog. Below is the summary of all changes since 0.13.8.
Gevent 1.0 supports Python 2.5 - 2.7. The version of greenlet required is 0.3.2. The source distribution
now includes the dependencies (libev and c-ares) and has not dependencies other than greenlet.
now includes the dependencies (libev and c-ares) and has no dependencies other than greenlet.
New core
~~~~~~~~
========
New event loop is used libev instead of libevent (see http://blog.gevent.org/2011/04/28/libev-and-libevent/ for motivation).
Now the event loop is using libev instead of libevent (see http://blog.gevent.org/2011/04/28/libev-and-libevent/ for motivation).
The new :mod:`gevent.core` has been rewritten to wrap libev's API. (On Windows, the :mod:`gevent.core` accepts Windows handles
rather than stdio file descriptors.).
...
...
@@ -26,7 +27,7 @@ Thus ``sys.exit()`` when run inside a greenlet is no longer trapped and kills th
New dns resolver
~~~~~~~~~~~~~~~~
================
Two new DNS resolvers: threadpool-based one (enabled by default) and c-ares based one. That threadpool-based resolver was added mostly for Windows and Mac OS X platforms where c-ares might behave differently w.r.t system configuration. On Linux, however, the c-ares based resolver is probably a better choice. To enable c-ares resolver set GEVENT_RESOLVER=ares environment variable.
...
...
@@ -46,7 +47,7 @@ It is possible to implement your own DNS resolver and make gevent use it. The GE
New API
~~~~~~~
=======
- :func:`gevent.wait` and :func:`gevent.iwait`
- UDP server: gevent.server.DatagramServer
...
...
@@ -66,10 +67,10 @@ New API
Breaking changes
~~~~~~~~~~~~~~~~
================
Removed features
^^^^^^^^^^^^^^^^
----------------
- gevent.dns module (wrapper around libevent-dns)
- gevent.http module (wrapper around libevent-http)
...
...
@@ -84,20 +85,20 @@ Renamed gevent.coros to gevent.lock. The gevent.coros is still available but dep
API changes
^^^^^^^^^^^
-----------
In all servers, method "kill" was renamed to "close". The old name is available as deprecated alias.
- ``Queue(0)`` is now equivalent to an unbound queue and raises :exc:`DeprecationError`. Use :class:`gevent.queue.Channel` if you need a channel.
The :class:`Greenlet` objects:
The :class:`gevent.Greenlet` objects:
- Added ``__nonzero__`` implementation that returns `True` after greenlet was started until it's dead. This overrides
greenlet's __nonzero__ which returned `False` after `start()` until it was first switched to.
Bugfixes
~~~~~~~~
========
- Issue #302: "python -m gevent.monkey" now sets __file__ properly.
- Issue #143: greenlet links are now executed in the order they were added