Skip to content

Commit 0bd4dc5

Browse files
committed
Update asyncio-daemon example and tutorial
1 parent 29d0900 commit 0bd4dc5

File tree

6 files changed

+80
-89
lines changed

6 files changed

+80
-89
lines changed

docs/tutorials/asyncio-daemon.rst

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Put next lines into the ``Dockerfile`` file:
135135

136136
.. code-block:: bash
137137
138-
FROM python:3.9-buster
138+
FROM python:3.10-buster
139139
140140
ENV PYTHONUNBUFFERED=1
141141
@@ -204,11 +204,11 @@ Logging and configuration
204204

205205
In this section we will configure the logging and configuration file parsing.
206206

207-
Let's start with the the main part of our application - the container. Container will keep all of
207+
Let's start with the the main part of our application the container. Container will keep all of
208208
the application components and their dependencies.
209209

210-
First two components that we're going to add are the config object and the provider for
211-
configuring the logging.
210+
First two components that we're going to add are the configuration provider and the resource provider
211+
for configuring the logging.
212212

213213
Put next lines into the ``containers.py`` file:
214214

@@ -224,7 +224,7 @@ Put next lines into the ``containers.py`` file:
224224
225225
class Container(containers.DeclarativeContainer):
226226
227-
config = providers.Configuration()
227+
config = providers.Configuration(yaml_files=["config.yml"])
228228
229229
logging = providers.Resource(
230230
logging.basicConfig,
@@ -233,26 +233,18 @@ Put next lines into the ``containers.py`` file:
233233
format=config.log.format,
234234
)
235235
236-
.. note::
237-
238-
We have used the configuration value before it was defined. That's the principle how the
239-
``Configuration`` provider works.
240-
241-
Use first, define later.
242-
243-
The configuration file will keep the logging settings.
244-
245-
Put next lines into the ``config.yml`` file:
236+
The configuration file will keep the logging settings. Put next lines into the ``config.yml`` file:
246237

247238
.. code-block:: yaml
248239
249240
log:
250241
level: "INFO"
251242
format: "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s"
252243
253-
Now let's create the function that will run our daemon. It's traditionally called
254-
``main()``. The ``main()`` function will create the container. Then it will use the container
255-
to parse the ``config.yml`` file and call the logging configuration provider.
244+
Now let's create the function that will run our daemon. It's traditionally called ``main()``.
245+
The ``main()`` function will start the dispatcher, but we will keep it empty for now.
246+
We will create the container instance before calling ``main()`` in ``if __name__ == "__main__"``.
247+
Container instance will parse ``config.yml`` and then we will call the logging configuration provider.
256248

257249
Put next lines into the ``__main__.py`` file:
258250

@@ -269,7 +261,6 @@ Put next lines into the ``__main__.py`` file:
269261
270262
if __name__ == "__main__":
271263
container = Container()
272-
container.config.from_yaml("config.yml")
273264
container.init_resources()
274265
275266
main()
@@ -419,7 +410,7 @@ Edit ``containers.py``:
419410
420411
class Container(containers.DeclarativeContainer):
421412
422-
config = providers.Configuration()
413+
config = providers.Configuration(yaml_files=["config.yml"])
423414
424415
logging = providers.Resource(
425416
logging.basicConfig,
@@ -442,7 +433,7 @@ and call the ``run()`` method. We will use :ref:`wiring` feature.
442433
Edit ``__main__.py``:
443434

444435
.. code-block:: python
445-
:emphasize-lines: 3-5,9-11,18
436+
:emphasize-lines: 3-5,9-11,17
446437
447438
"""Main module."""
448439
@@ -459,7 +450,6 @@ Edit ``__main__.py``:
459450
460451
if __name__ == "__main__":
461452
container = Container()
462-
container.config.from_yaml("config.yml")
463453
container.init_resources()
464454
container.wire(modules=[__name__])
465455
@@ -559,7 +549,7 @@ Edit ``containers.py``:
559549
560550
class Container(containers.DeclarativeContainer):
561551
562-
config = providers.Configuration()
552+
config = providers.Configuration(yaml_files=["config.yml"])
563553
564554
logging = providers.Resource(
565555
logging.basicConfig,
@@ -664,7 +654,7 @@ Edit ``containers.py``:
664654
665655
class Container(containers.DeclarativeContainer):
666656
667-
config = providers.Configuration()
657+
config = providers.Configuration(yaml_files=["config.yml"])
668658
669659
logging = providers.Resource(
670660
logging.basicConfig,
@@ -763,7 +753,7 @@ Edit ``containers.py``:
763753
764754
class Container(containers.DeclarativeContainer):
765755
766-
config = providers.Configuration()
756+
config = providers.Configuration(yaml_files=["config.yml"])
767757
768758
logging = providers.Resource(
769759
logging.basicConfig,
@@ -888,7 +878,7 @@ Create ``tests.py`` in the ``monitoringdaemon`` package:
888878
and put next into it:
889879

890880
.. code-block:: python
891-
:emphasize-lines: 54,70-71
881+
:emphasize-lines: 54,70-73
892882
893883
"""Tests module."""
894884
@@ -909,28 +899,28 @@ and put next into it:
909899
910900
@pytest.fixture
911901
def container():
912-
container = Container()
913-
container.config.from_dict({
914-
"log": {
915-
"level": "INFO",
916-
"formant": "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s",
917-
},
918-
"monitors": {
919-
"example": {
920-
"method": "GET",
921-
"url": "http://fake-example.com",
922-
"timeout": 1,
923-
"check_every": 1,
902+
return Container(
903+
config={
904+
"log": {
905+
"level": "INFO",
906+
"formant": "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s",
924907
},
925-
"httpbin": {
926-
"method": "GET",
927-
"url": "https://fake-httpbin.org/get",
928-
"timeout": 1,
929-
"check_every": 1,
908+
"monitors": {
909+
"example": {
910+
"method": "GET",
911+
"url": "http://fake-example.com",
912+
"timeout": 1,
913+
"check_every": 1,
914+
},
915+
"httpbin": {
916+
"method": "GET",
917+
"url": "https://fake-httpbin.org/get",
918+
"timeout": 1,
919+
"check_every": 1,
920+
},
930921
},
931-
},
932-
})
933-
return container
922+
}
923+
)
934924
935925
936926
@pytest.mark.asyncio
@@ -959,9 +949,10 @@ and put next into it:
959949
example_monitor_mock = mock.AsyncMock()
960950
httpbin_monitor_mock = mock.AsyncMock()
961951
962-
with container.example_monitor.override(example_monitor_mock), \
963-
container.httpbin_monitor.override(httpbin_monitor_mock):
964-
952+
with container.override_providers(
953+
example_monitor=example_monitor_mock,
954+
httpbin_monitor=httpbin_monitor_mock,
955+
):
965956
dispatcher = container.dispatcher()
966957
event_loop.create_task(dispatcher.start())
967958
await asyncio.sleep(0.1)
@@ -980,25 +971,25 @@ You should see:
980971

981972
.. code-block:: bash
982973
983-
platform linux -- Python 3.9, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
974+
platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
984975
rootdir: /code
985-
plugins: asyncio-0.14.0, cov-2.10.0
976+
plugins: asyncio-0.16.0, cov-3.0.0
986977
collected 2 items
987978
988979
monitoringdaemon/tests.py .. [100%]
989980
990-
----------- coverage: platform linux, python 3.9 -----------
981+
---------- coverage: platform linux, python 3.10.0-final-0 -----------
991982
Name Stmts Miss Cover
992983
----------------------------------------------------
993984
monitoringdaemon/__init__.py 0 0 100%
994-
monitoringdaemon/__main__.py 13 13 0%
985+
monitoringdaemon/__main__.py 11 11 0%
995986
monitoringdaemon/containers.py 11 0 100%
996-
monitoringdaemon/dispatcher.py 44 5 89%
987+
monitoringdaemon/dispatcher.py 45 5 89%
997988
monitoringdaemon/http.py 6 3 50%
998989
monitoringdaemon/monitors.py 23 1 96%
999-
monitoringdaemon/tests.py 37 0 100%
990+
monitoringdaemon/tests.py 35 0 100%
1000991
----------------------------------------------------
1001-
TOTAL 134 22 84%
992+
TOTAL 131 20 85%
1002993
1003994
.. note::
1004995

examples/miniapps/asyncio-daemon/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.9-buster
1+
FROM python:3.10-buster
22

33
ENV PYTHONUNBUFFERED=1
44

examples/miniapps/asyncio-daemon/README.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,22 @@ The output should be something like:
6565

6666
.. code-block::
6767
68-
platform linux -- Python 3.9, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
68+
platform linux -- Python 3.10.0, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
6969
rootdir: /code
70-
plugins: asyncio-0.14.0, cov-2.10.0
70+
plugins: asyncio-0.16.0, cov-3.0.0
7171
collected 2 items
7272
7373
monitoringdaemon/tests.py .. [100%]
7474
75-
----------- coverage: platform linux, python 3.9 -----------
75+
---------- coverage: platform linux, python 3.10.0-final-0 -----------
7676
Name Stmts Miss Cover
7777
----------------------------------------------------
7878
monitoringdaemon/__init__.py 0 0 100%
79-
monitoringdaemon/__main__.py 13 13 0%
79+
monitoringdaemon/__main__.py 11 11 0%
8080
monitoringdaemon/containers.py 11 0 100%
81-
monitoringdaemon/dispatcher.py 44 5 89%
81+
monitoringdaemon/dispatcher.py 45 5 89%
8282
monitoringdaemon/http.py 6 3 50%
8383
monitoringdaemon/monitors.py 23 1 96%
84-
monitoringdaemon/tests.py 37 0 100%
84+
monitoringdaemon/tests.py 35 0 100%
8585
----------------------------------------------------
86-
TOTAL 134 22 84%
86+
TOTAL 131 20 85%

examples/miniapps/asyncio-daemon/monitoringdaemon/__main__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ def main(dispatcher: Dispatcher = Provide[Container.dispatcher]) -> None:
1313

1414
if __name__ == "__main__":
1515
container = Container()
16-
container.config.from_yaml("config.yml")
1716
container.init_resources()
1817
container.wire(modules=[__name__])
1918

examples/miniapps/asyncio-daemon/monitoringdaemon/containers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class Container(containers.DeclarativeContainer):
1212

13-
config = providers.Configuration()
13+
config = providers.Configuration(yaml_files=["config.yml"])
1414

1515
logging = providers.Resource(
1616
logging.basicConfig,

examples/miniapps/asyncio-daemon/monitoringdaemon/tests.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@ class RequestStub:
1717

1818
@pytest.fixture
1919
def container():
20-
container = Container()
21-
container.config.from_dict({
22-
"log": {
23-
"level": "INFO",
24-
"formant": "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s",
25-
},
26-
"monitors": {
27-
"example": {
28-
"method": "GET",
29-
"url": "http://fake-example.com",
30-
"timeout": 1,
31-
"check_every": 1,
20+
return Container(
21+
config={
22+
"log": {
23+
"level": "INFO",
24+
"formant": "[%(asctime)s] [%(levelname)s] [%(name)s]: %(message)s",
3225
},
33-
"httpbin": {
34-
"method": "GET",
35-
"url": "https://fake-httpbin.org/get",
36-
"timeout": 1,
37-
"check_every": 1,
26+
"monitors": {
27+
"example": {
28+
"method": "GET",
29+
"url": "http://fake-example.com",
30+
"timeout": 1,
31+
"check_every": 1,
32+
},
33+
"httpbin": {
34+
"method": "GET",
35+
"url": "https://fake-httpbin.org/get",
36+
"timeout": 1,
37+
"check_every": 1,
38+
},
3839
},
39-
},
40-
})
41-
return container
40+
}
41+
)
4242

4343

4444
@pytest.mark.asyncio
@@ -67,9 +67,10 @@ async def test_dispatcher(container, caplog, event_loop):
6767
example_monitor_mock = mock.AsyncMock()
6868
httpbin_monitor_mock = mock.AsyncMock()
6969

70-
with container.example_monitor.override(example_monitor_mock), \
71-
container.httpbin_monitor.override(httpbin_monitor_mock):
72-
70+
with container.override_providers(
71+
example_monitor=example_monitor_mock,
72+
httpbin_monitor=httpbin_monitor_mock,
73+
):
7374
dispatcher = container.dispatcher()
7475
event_loop.create_task(dispatcher.start())
7576
await asyncio.sleep(0.1)

0 commit comments

Comments
 (0)