summaryrefslogtreecommitdiff
path: root/doc/using.rst
blob: c730f86b8233424351df7e283ccaedc86830052a (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
++++++++++++++
Using Trollius
++++++++++++++

Documentation of the asyncio module
===================================

The documentation of the asyncio is part of the Python project. It can be read
online: `asyncio - Asynchronous I/O, event loop, coroutines and tasks
<http://docs.python.org/dev/library/asyncio.html>`_.

To adapt asyncio examples for Trollius, "just":

* replace ``asyncio`` with ``trollius``
  (or use ``import trollius as asyncio``)
* replace ``yield from ...`` with ``yield From(...)``
* replace ``yield from []`` with ``yield From(None)``
* in coroutines, replace ``return res`` with ``raise Return(res)``


Trollius Hello World
====================

Print ``Hello World`` every two seconds, using a coroutine::

    import trollius
    from trollius import From

    @trollius.coroutine
    def greet_every_two_seconds():
        while True:
            print('Hello World')
            yield From(trollius.sleep(2))

    loop = trollius.get_event_loop()
    loop.run_until_complete(greet_every_two_seconds())


Debug mode
==========

To enable the debug mode:

* Set ``TROLLIUSDEBUG`` envrironment variable to ``1``
* Configure logging to log at level ``logging.DEBUG``,
  ``logging.basicConfig(level=logging.DEBUG)`` for example

The ``BaseEventLoop.set_debug()`` method can be used to set the debug mode on a
specific event loop. The environment variable enables also the debug mode for
coroutines.

Effect of the debug mode:

* On Python 2, :meth:`Future.set_exception` stores the traceback, so
  ``loop.run_until_complete()`` raises the exception with the original
  traceback.
* Log coroutines defined but never "yielded"
* BaseEventLoop.call_soon() and BaseEventLoop.call_at() methods raise an
  exception if they are called from the wrong thread.
* Log the execution time of the selector
* Log callbacks taking more than 100 ms to be executed. The
  BaseEventLoop.slow_callback_duration attribute is the minimum duration in
  seconds of "slow" callbacks.
* Log most important subprocess events:

  - Log stdin, stdout and stderr transports and protocols
  - Log process identifier (pid)
  - Log connection of pipes
  - Log process exit
  - Log Process.communicate() tasks: feed stdin, read stdout and stderr

* Log most important socket events:

  - Socket connected
  - New client (socket.accept())
  - Connection reset or closed by peer (EOF)
  - Log time elapsed in DNS resolution (getaddrinfo)
  - Log pause/resume reading
  - Log time of SSL handshake
  - Log SSL handshake errors

See `Debug mode of asyncio
<https://docs.python.org/dev/library/asyncio-dev.html#debug-mode-of-asyncio>`_
for more information.