summaryrefslogtreecommitdiff
path: root/doc/using.rst
blob: 827d2ad592a2eb18cfa25317e6620eef806bab72 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
Usage
=====

Use aiogreen with trollius
--------------------------

aiogreen can be used with trollius, coroutines written with ``yield
From(...)``. Using aiogreen with trollius is a good start to port project
written for eventlet to trollius.

To use aiogreen with trollius, set the event loop policy before using an event
loop, example::

    import aiogreen
    import trollius

    trollius.set_event_loop_policy(aiogreen.EventletEventLoopPolicy())
    # ....

Hello World::

    import aiogreen
    import trollius as asyncio

    def hello_world():
        print("Hello World")
        loop.stop()

    asyncio.set_event_loop_policy(aiogreen.EventletEventLoopPolicy())
    loop = asyncio.get_event_loop()
    loop.call_soon(hello_world)
    loop.run_forever()
    loop.close()

.. seealso::
   `Trollius documentation <http://trollius.readthedocs.org/>`_.


Use aiogreen with asyncio
-------------------------

aiogreen can be used with asyncio, coroutines written with ``yield from ...``.
To use aiogreen with asyncio, set the event loop policy before using an event
loop. Example::

    import aiogreen
    import asyncio

    asyncio.set_event_loop_policy(aiogreen.EventletEventLoopPolicy())
    # ....

Setting the event loop policy should be enough to examples of the asyncio
documentation with the aiogreen event loop.

.. warning::
   Since aiogreen relies on eventlet, eventlet port to Python 3 is not complete
   and asyncio requires Python 3.3 or newer: using aiogreen with asyncio is not
   recommended yet. *Using aiogreen with trollius should be preferred right
   now*.  See the :ref:`status of the eventlet port to Python 3
   <eventlet-py3>`.

Hello World::

    import aiogreen
    import asyncio

    def hello_world():
        print("Hello World")
        loop.stop()

    asyncio.set_event_loop_policy(aiogreen.EventletEventLoopPolicy())
    loop = asyncio.get_event_loop()
    loop.call_soon(hello_world)
    loop.run_forever()
    loop.close()

.. seealso::
   The `asyncio documentation
   <https://docs.python.org/dev/library/asyncio.html>`_.


Threads
-------

Running an event loop in a thread different than the main thread is currently
experimental.

An eventlet Event object is not thread-safe, it must only be used in the
same thread. Use threading.Event to signal events between threads,
and threading.Queue to pass data between threads.

Use ``threading = eventlet.patcher.original('threading')`` to get the original
threading instead of ``import threading``.

It is not possible to run two aiogreen event loops in the same thread.


Debug mode
----------

To enable the debug mode globally when using trollius, set the environment
variable ``TROLLIUSDEBUG`` to ``1``. To see debug traces, set the log level of
the trollius logger to ``logging.DEBUG``.  The simplest configuration is::

   import logging
   # ...
   logging.basicConfig(level=logging.DEBUG)

If you use asyncio,  use the ``PYTHONASYNCIODEBUG`` environment variable
instead of the ``TROLLIUSDEBUG`` variable.

You can also call ``loop.set_debug(True)`` to enable the debug mode of the
event loop, but it enables less debug checks.

.. seealso::
   Read the `Develop with asyncio
   <https://docs.python.org/dev/library/asyncio-dev.html>`_ section of the
   asyncio documentation.


API
===

aiogreen specific functions:

.. warning::
   aiogreen API is not considered as stable yet.

EventletEventLoop
-----------------

.. class:: EventletEventLoop

   asyncio or trollius event loop running on top of eventlet.

   The API is the API of asyncio event loop, with one extra method: :meth:`link_future`.

   .. versionchanged:: 0.4

      The class was renamed from ``EventLoop`` to ``EventletEventLoop``.

   .. method:: link_future(future)

      Wait for a future, a task, or a coroutine object from a greenthread.

      Return the result or raise the exception of the future.

      The function must not be called from the greenthread of the aiogreen event
      loop.

      .. versionchanged:: 0.4

         The function was converted to a method of the
         :class:`EventletEventLoop` class.

      .. versionchanged:: 0.3

         Coroutine objects are also accepted. Added the *loop* parameter.
         An exception is raised if it is called from the greenthread of the
         aiogreen event loop.

      Example of greenthread waiting for a trollius task. The ``progress()``
      callback is called regulary to see that the event loop in not blocked::

           import aiogreen
           import eventlet
           import trollius as asyncio
           from trollius import From, Return

           def progress():
               print("computation in progress...")
               loop.call_later(0.5, progress)

           @asyncio.coroutine
           def coro_slow_sum(x, y):
               yield From(asyncio.sleep(1.0))
               raise Return(x + y)

           def green_sum():
               loop.call_soon(progress)

               task = asyncio.async(coro_slow_sum(1, 2))

               value = loop.link_future(task)
               print("1 + 2 = %s" % value)

               loop.stop()

           asyncio.set_event_loop_policy(aiogreen.EventletEventLoopPolicy())
           eventlet.spawn(green_sum)
           loop = asyncio.get_event_loop()
           loop.run_forever()
           loop.close()

      Output::

           computation in progress...
           computation in progress...
           computation in progress...
           1 + 2 = 3

wrap_greenthread
----------------

.. function:: wrap_greenthread(gt)

   Wrap an eventlet GreenThread, or a greenlet, into a Future object.

   The Future object waits for the completion of a greenthread. The result or
   the exception of the greenthread will be stored in the Future object.

   The greenthread must be wrapped before its execution starts.  If the
   greenthread is running or already finished, an exception is raised.

   For greenlets, the ``run`` attribute must be set.

   .. versionchanged:: 0.3

     An exception is now raised if the greenthread is running or already
     finished. In debug mode, the exception is not more logged to sys.stderr
     for greenthreads.

   Example of trollius coroutine waiting for a greenthread. The ``progress()``
   callback is called regulary to see that the event loop in not blocked::

        import aiogreen
        import eventlet
        import trollius as asyncio
        from trollius import From, Return

        def progress():
            print("computation in progress...")
            loop.call_later(0.5, progress)

        def slow_sum(x, y):
            eventlet.sleep(1.0)
            return x + y

        @asyncio.coroutine
        def coro_sum():
            loop.call_soon(progress)

            gt = eventlet.spawn(slow_sum, 1, 2)
            fut = aiogreen.wrap_greenthread(gt, loop=loop)

            result = yield From(fut)
            print("1 + 2 = %s" % result)

        asyncio.set_event_loop_policy(aiogreen.EventletEventLoopPolicy())
        loop = asyncio.get_event_loop()
        loop.run_until_complete(coro_sum())
        loop.close()

   Output::

        computation in progress...
        computation in progress...
        computation in progress...
        1 + 2 = 3


EventletEventLoopPolicy
-----------------------

.. class:: EventletEventLoopPolicy

   Event loop policy creating :class:`EventletEventLoop` event loops.


Installation
============

Install aiogreen with pip
-------------------------

Type::

    pip install aiogreen

Install aiogreen on Windows with pip
------------------------------------

Procedure for Python 2.7:

* If pip is not installed yet, `install pip
  <http://www.pip-installer.org/en/latest/installing.html>`_: download
  ``get-pip.py`` and type::

  \Python27\python.exe get-pip.py

* Install aiogreen with pip::

  \Python27\python.exe -m pip install aiogreen

* pip also installs dependencies: ``eventlet`` and ``trollius``

Manual installation of aiogreen
-------------------------------

Requirements:

- eventlet 0.14 or newer
- asyncio or trollius:

  * Python 3.4 and newer: asyncio is now part of the stdlib (only eventlet is
    needed)
  * Python 3.3: need Tulip 0.4.1 or newer (``pip install asyncio``),
    but Tulip 3.4.1 or newer is recommended
  * Python 2.6-3.2: need Trollius 0.3 or newer (``pip install trollius``),
    but Trollius 1.0 or newer is recommended

Type::

    python setup.py install


Run tests
=========

Run tests with tox
------------------

The `tox project <http://testrun.org/tox/latest/>`_ can be used to build a
virtual environment with all runtime and test dependencies and run tests
against different Python versions (2.6, 2.7, 3.2, 3.3, 3.4).

To test all Python versions, just type::

    tox

To run tests with Python 2.7, type::

    tox -e py27

To run tests against other Python versions:

* ``py26``: Python 2.6
* ``py27``: Python 2.7
* ``py27_patch``: Python 2.7 with eventlet monkey patching
* ``py27_old``: Python 2.7 with the oldest supported versions of eventlet and
  trollius
* ``py32``: Python 3.2
* ``py33``: Python 3.3
* ``py33_old``: Python 3.3 with the oldest supported versions of eventlet and
  tulip
* ``py34``: Python 3.4

Run tests manually
------------------

To run unit tests, the ``mock`` module is need on Python older than 3.3.

Run the following command::

    python runtests.py -r