|
1 | 1 | from twisted.internet import defer, reactor
|
2 | 2 |
|
3 | 3 |
|
4 |
| -def get_dummy_data(input_data): |
| 4 | +def get_dummy_number( |
| 5 | + input_number: int, defer_duration: float = 2, multiplier: float = 3 |
| 6 | +) -> defer.Deferred: |
5 | 7 | """
|
6 | 8 | This function is a dummy which simulates a delayed result and
|
7 |
| - returns a Deferred which will fire with that result. Don't try too |
8 |
| - hard to understand this. |
| 9 | + returns a Deferred which will fire with that result. |
9 | 10 | """
|
10 |
| - print("get_dummy_data called") |
| 11 | + |
| 12 | + print("get_dummy_number called") |
| 13 | + |
11 | 14 | deferred = defer.Deferred()
|
12 |
| - # simulate a delayed result by asking the reactor to fire the |
13 |
| - # Deferred in 2 seconds time with the result inputData * 3 |
14 |
| - reactor.callLater(2, deferred.callback, input_data * 3) |
| 15 | + # Simulate a delayed result by asking the reactor to fire the |
| 16 | + # Deferred in '2 seconds' time with the result 'input_number * 3'. |
| 17 | + reactor.callLater(defer_duration, deferred.callback, input_number * multiplier) |
15 | 18 | return deferred
|
16 | 19 |
|
17 | 20 |
|
18 |
| -def cb_print_data(result): |
| 21 | +def cb_print_number(result: int) -> None: |
19 | 22 | """
|
20 | 23 | Data handling function to be added as a callback: handles the
|
21 | 24 | data by printing the result
|
22 | 25 | """
|
23 | 26 | print(f"Result received: {result}")
|
24 | 27 |
|
25 | 28 |
|
26 |
| -deferred = get_dummy_data(3) |
27 |
| -deferred.addCallback(cb_print_data) |
| 29 | +def orchestrator(stop_after: float = 4) -> None: |
| 30 | + deferred = get_dummy_number(3) |
| 31 | + deferred.addCallback(cb_print_number) |
| 32 | + |
| 33 | + # Manually set up the end of the process by asking the reactor to |
| 34 | + # stop itself in 4 seconds time. |
| 35 | + reactor.callLater(stop_after, reactor.stop) |
| 36 | + |
| 37 | + # Start up the Twisted reactor (event loop handler) manually. |
| 38 | + print("Starting the reactor") |
| 39 | + reactor.run() |
| 40 | + |
28 | 41 |
|
29 |
| -# manually set up the end of the process by asking the reactor to |
30 |
| -# stop itself in 4 seconds time |
31 |
| -reactor.callLater(4, reactor.stop) |
32 |
| -# start up the Twisted reactor (event loop handler) manually |
33 |
| -print("Starting the reactor") |
34 |
| -reactor.run() |
| 42 | +if __name__ == "__main__": |
| 43 | + orchestrator() |
0 commit comments