-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_main.py
More file actions
62 lines (47 loc) · 1.76 KB
/
sync_main.py
File metadata and controls
62 lines (47 loc) · 1.76 KB
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.sync_main
This is an example of using Effect in a normal program that uses
synchronous/blocking functions to do I/O.
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:`effect.sync_perform` to perform an effect, which returns the
result of the effect (or raises an exception it it failed).
"""
from __future__ import print_function
from functools import partial
from multiprocessing.pool import ThreadPool
from effect import ComposedDispatcher, ParallelEffects, TypeDispatcher, sync_perform
from effect.threads import perform_parallel_with_pool
from ehttp.http_intent import HTTPRequest
from ehttp.sync_http import perform_request_requests
from readline_intent import ReadLine, perform_readline_stdin
from .core import main_effect
def get_dispatcher():
"""
Create a dispatcher that can find performers for :obj:`ReadLine`,
:obj:`HTTPRequest`, and :obj:`ParallelEffects`. There's a built-in
performer for ParallelEffects that uses a multiprocessing ThreadPool,
:func:`effect.perform_parallel_with_pool`.
"""
my_pool = ThreadPool()
pool_performer = partial(perform_parallel_with_pool, my_pool)
return ComposedDispatcher(
[
TypeDispatcher(
{
ReadLine: perform_readline_stdin,
HTTPRequest: perform_request_requests,
ParallelEffects: pool_performer,
}
)
]
)
def main():
dispatcher = get_dispatcher()
eff = main_effect()
print(sync_perform(dispatcher, eff))
if __name__ == "__main__":
main()