/
twisted_main.py
62 lines (47 loc) · 1.84 KB
/
twisted_main.py
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
"""
Run this example with:
python -m github.twisted_main
This is an example of using Effect with Twisted.
It's important to note that none of the application code relies on Twisted --
this is the only file that has any dependencies on Twisted. There's also an
example of running the same application code in ``sync_main.py`` in the same
directory.
This code has these responsibilities:
- set up a dispatcher that knows how to find performers for all intents
used in this application. The application uses ReadLine, HTTPRequest, and
ParallelEffects.
- use :func:`texeffect.perform` to perform an effect, which returns a
Deferred that we can return from our react function.
"""
from __future__ import print_function
from twisted.internet.task import react
from txeffect import make_twisted_dispatcher, perform
from effect import ComposedDispatcher, TypeDispatcher
from ehttp.http_intent import HTTPRequest
from ehttp.twisted_http import perform_request_with_treq
from readline_intent import ReadLine, perform_readline_stdin
from .core import main_effect
def get_dispatcher(reactor):
"""
Create a dispatcher that can find performers for :obj:`ReadLine`,
:obj:`HTTPRequest`, and :obj:`ParallelEffects`.
:func:`make_twisted_dispatcher` is able to provide the ``ParallelEffects``
performer, so we compose it with our own custom :obj:`TypeDispatcher`.
"""
return ComposedDispatcher(
[
TypeDispatcher(
{
ReadLine: perform_readline_stdin,
HTTPRequest: perform_request_with_treq,
}
),
make_twisted_dispatcher(reactor),
]
)
def main(reactor):
dispatcher = get_dispatcher(reactor)
eff = main_effect()
return perform(dispatcher, eff).addCallback(print)
if __name__ == "__main__":
react(main, [])