Skip to content

Commit 7cad0dd

Browse files
committed
Add async docs
1 parent a324cfc commit 7cad0dd

File tree

5 files changed

+250
-2
lines changed

5 files changed

+250
-2
lines changed

source/_includes/asides/developers_navigation.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,16 @@ <h1 class="title delta">Development Guide</h1>
4343
</ul>
4444
</li>
4545
<li>
46-
Frontend Development
46+
{% active_link /developers/asyncio/ Asynchronous Programming %}
47+
<ul>
48+
<li>{% active_link /developers/asyncio_categorizing_functions/ Categorizing Functions %}</li>
49+
<li>{% active_link /developers/asyncio_working_with_async/ Working with Async %}</li>
50+
<li>{% active_link /developers/asyncio_misc/ Miscellaneous %}</li>
51+
</ul>
52+
</li>
53+
<li>
54+
{% active_link /developers/frontend/ Frontend Development %}
4755
<ul>
48-
<li>{% active_link /developers/frontend/ Setup Frontend Environment %}</li>
4956
<li>{% active_link /developers/frontend_add_card/ Add State Card %}</li>
5057
<li>{% active_link /developers/frontend_add_more_info/ Add More Info Dialog %}</li>
5158
<li>{% active_link /developers/frontend_creating_custom_panels/ Add Custom Panels %}</li>

source/developers/asyncio.markdown

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
layout: page
3+
title: "Asynchronous Programming"
4+
description: "Introduction to the asynchronous core of Home Assistant."
5+
date: 2016-10-17 21:49
6+
sidebar: true
7+
comments: false
8+
sharing: true
9+
footer: true
10+
---
11+
12+
On September 29, 2016 we released [Home Assistant 0.29][0.29] as part of our bi-weekly release schedule. This release introduced a complete overhaul of the core spearheaded by [Ben Bangert][ben].
13+
14+
The old core was set up like a “traditional” threaded application. Each resource that was not thread safe (ie. the state of entities) would be protected by a lock. This caused a lot of waiting and potential inconsistency because a task could now end up waiting halfway through it’s job until some resource got freed.
15+
16+
Our new core is based on an Python’s built-in asyncio module. Instead of having all threads have access to the core API objects, access is now limited to a special thread called the event loop. All components will now schedule themselves as a task to be executed by the event loop. This gives us the guarantee that only one task is executed at once, meaning we no longer need any locks.
17+
18+
The only problem with running everything inside the event loop is when a task is doing blocking I/O, what most third-party Python libraries are doing. For example while requesting new information from a device, the core will stop running until we get a response from the device. To handle this, a task is able to suspend itself until the response is available after which it will be enqueued for the event loop to process the result.
19+
20+
For a task to be able to suspend itself, all code that it calls has to have this capability added. This means in practice that each device integration will need a full rewrite of the library that offers the integration! As this is not something that can be achieved, ever, a 100% backwards compatible API has been added so that no platform will require updating.
21+
22+
The backwards compatible API works by scheduling a task from a different thread and blocking that thread until the task has been processed by the event loop.
23+
24+
### [Next step: Categorizing Functions &raquo;](/developers/asyncio_categorizing_functions/)
25+
26+
[0.29]: https://home-assistant.io/blog/2016/09/29/async-sleepiq-emoncms-stocks/
27+
[ben]: https://github.com/bbangert/
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
layout: page
3+
title: "Categorizing Functions"
4+
description: "A categorization of functions to work with the asynchronous core of Home Assistant."
5+
date: 2016-10-17 21:49
6+
sidebar: true
7+
comments: false
8+
sharing: true
9+
footer: true
10+
---
11+
12+
A piece of work within Home Assistant is represented by a function that will be invoked. It will either run inside our event loop or inside our thread pool, depending on if it is async safe.
13+
14+
Home Assistant uses the convention that all functions that must be run from within the event loop are prefixed with `async_`.
15+
16+
## {% linkable_title The coroutine function %}
17+
18+
Coroutines are special functions based on Python’s generators syntax which allows them to suspend execution while waiting on a result.
19+
20+
Invoking a coroutine function will return a Generator object back, but will not actually begin execution. This object will execute the task when it is either yielded from (from within another coroutine) or it is scheduled on the event loop.
21+
22+
To declare a function a coroutine, import the coroutine annotation from the asyncio package and annotate your function.
23+
24+
```python
25+
import asyncio
26+
27+
@asyncio.coroutine
28+
def async_look_my_coroutine(target):
29+
result = yield from entity.async_turn_on()
30+
if result:
31+
print("hello {}".format(target))
32+
33+
hass.loop.create_task(async_look_my_coroutine("world")
34+
```
35+
36+
In this example, we schedule the coroutine by calling `hass.loop.create_task`. This will add the coroutine to the queue of tasks to be run. When the event loop is running `async_look_my_coroutine` it will suspend the task when `yield from entity.async_turn_on()` is called. At that point a new task will be scheduled to execute `entity.async_turn_on()`. When that job has been executed, `async_look_my_coroutine` will resume.
37+
38+
## {% linkable_title The callback function %}
39+
40+
This is a normal function that is considered safe to be run from within the event loop. A callback is unable to suspend itself and thus cannot do any I/O or call a coroutine. A callback is capable of scheduling a new task but it will not be able to wait for the results.
41+
42+
To declare a function as a callback, import the callback annotation from the core package and annotate your function.
43+
44+
A common use case for a callback in Home Assistant is as a listener for an event or a service call. It can process the incoming information and then schedule the right calls to be made. Example from the automation component.
45+
46+
```python
47+
from homeassistant.core import callback
48+
49+
@callback
50+
def async_trigger_service_handler(service_call):
51+
"""Handle automation trigger service calls."""
52+
vars = service_call.data.get(ATTR_VARIABLES)
53+
for entity in component.async_extract_from_service(service_call):
54+
hass.loop.create_task(entity.async_trigger(vars, True))
55+
```
56+
57+
In this example, `entity.async_trigger` is a coroutine function. Invoking the coroutine function will return a coroutine task. The passed in parameters will be used when the task gets executed.
58+
59+
To execute the task we have to schedule it for execution on the event loop. This is done by calling `hass.loop.create_task`.
60+
61+
### {% linkable_title Why even have callbacks? %}
62+
63+
You might wonder, if a coroutine can do everything a callback can do, why even have a callback. The reason is performance and better state consistency of the core API objects.
64+
65+
When coroutine A waits for coroutine B, it will suspend itself and schedule a new task to run B. This means that the event loop is now running A, B and then A again. If B is a callback, A will never have to suspend itself and thus the event loop is just running A. The consistency implication is that other events queued to run on the event loop continue to wait until callbacks complete, but will be interleaved when yielding to another coroutine.
66+
67+
## {% linkable_title Event loop and thread safe %}
68+
69+
These are functions that are safe to run both in a thread and inside the event loop. These functions are usually performing a computation or transform data in memory. Anything that does I/O does not fall under this category. Many standard library functions fall in this category. For example generating the sum of a set of numbers using sum or merging two dictionaries.
70+
71+
There is no special annotation to mark functions as part of this category and care should be taken when using these functions from inside the event loop. When in doubt, look at their implementation.
72+
73+
## {% linkable_title Other functions %}
74+
75+
These are all the functions that did not fit in the previous categories. These functions are either thread-safe or not considered safe to be run within the event loop. These are functions that use sleep, or perform I/O.
76+
77+
There is no special annotation necessary to be considered part of this category.
78+
79+
### [Next step: Working with Async &raquo;](/developers/asyncio_working_with_async/)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
layout: page
3+
title: "Miscellaneous Async"
4+
description: "A collection of miscellaneous topics about async that didn't fit on the other pages."
5+
date: 2016-10-17 21:49
6+
sidebar: true
7+
comments: false
8+
sharing: true
9+
footer: true
10+
---
11+
12+
## {% linkable_title What about ‘async’ and ‘await’ syntax? %}
13+
Python 3.5 introduced new syntax to formalize the asynchronous pattern. This is however not compatible with Python 3.4. The minimum required Python version for Home Assistant is based on the Python version shipped with Debian stable, which is currently 3.4.2.
14+
15+
For more information, Brett Cannon wrote [an excellent breakdown][brett] on 'async' and 'await' syntax and how asynchronous programming works.
16+
17+
## {% linkable_title Acknowledgements %}
18+
19+
Huge thanks to [Ben Bangert][ben] for starting the conversion of the core to async, guiding other contributors while taking their first steps with async programming and peer reviewing this documentation.
20+
21+
[brett]: http://www.snarky.ca/how-the-heck-does-async-await-work-in-python-3-5
22+
[ben]: https://github.com/bbangert/
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
layout: page
3+
title: "Working with Async"
4+
description: "A breakdown of all the different ways to work with the asynchronous core of Home Assistant."
5+
date: 2016-10-17 21:49
6+
sidebar: true
7+
comments: false
8+
sharing: true
9+
footer: true
10+
---
11+
12+
Although we have a backwards compatible API, using the async core directly will be a lot faster. Most core components have already been rewritten to leverage the async core. This includes the EntityComponent helper (foundation of light, switch, etc), scripts, groups and automation.
13+
14+
## {% linkable_title Interacting with the core %}
15+
16+
[All methods in the Home Assistant core][dev-docs] are implemented in two flavors: an async version and a version to be called from other threads. The versions for other are merely wrappers that call the async version in a threadsafe manner using [the available async utilities][dev-docs-async].
17+
18+
So if you are making calls to the core (the hass object) from within a callback or coroutine, use the methods that start with async_. If you need to call an async_ function that is a coroutine, your task must also be a coroutine.
19+
20+
## {% linkable_title Implementing an async component %}
21+
22+
We currently do not support async setup for components. We do however support using async functions as service handlers. Just define your handlers as a callback or coroutine and register them as usual.
23+
24+
## {% linkable_title Implementing an async platform %}
25+
26+
For platforms we support async setup. Instead of setup_platform you need to have a coroutine async_setup_platform.
27+
28+
```python
29+
setup_platform(hass, config, add_entities, discovery_info=None):
30+
# Setup your platform outside of the event loop.
31+
```
32+
33+
Will turn into:
34+
35+
```python
36+
import asyncio
37+
38+
@asyncio.coroutine
39+
def async_setup_platform(hass, config, async_add_entities,
40+
discovery_info=None):
41+
# Setup your platform inside of the event loop
42+
```
43+
44+
The only difference with the original parameters is that the add_entities function has been replaced by the coroutine `async_add_entities`.
45+
46+
## {% linkable_title Implementing an async entity %}
47+
48+
You can make your entity async friendly by converting your update method to be async. This requires the dependency of your entities to also be async friendly!
49+
50+
```python
51+
class MyEntity(Entity):
52+
def update(self):
53+
"""Retrieve latest state."""
54+
self._state = fetch_state()
55+
```
56+
57+
Will turn into:
58+
59+
```python
60+
import asyncio
61+
62+
class MyEntity(Entity):
63+
@asyncio.coroutine
64+
def async_update(self):
65+
"""Retrieve latest state."""
66+
self._state = yield from async_fetch_state()
67+
```
68+
69+
Make sure that all properties defined on your entity do not result in I/O being done. All data has to be fetched inside the update method and cached on the entity. This is because these properties are read from within the event loop and thus doing I/O will result in the core of Home Assistant waiting until your I/O is done.
70+
71+
## {% linkable_title Calling async functions from threads %}
72+
73+
Sometimes it will happen that you’re in a thread and you want to call a function that is only available as async. Home Assistant includes a few async helper utilities to help with this.
74+
75+
In the following example, `say_hello` will schedule `async_say_hello` and block till the function has run and get the result back.
76+
77+
```python
78+
from homeassistant.util.async import run_callback_threadsafe
79+
80+
def say_hello(hass, target):
81+
return run_callback_threadsafe(
82+
hass.loop, async_say_hello, target).result()
83+
84+
def async_say_hello(hass, target):
85+
return "Hello {}!".format(target)
86+
```
87+
88+
## {% linkable_title Dealing with passed in functions %}
89+
90+
If your code takes in functions from other code, you will not know which category the function belongs to and how they should be invoked. This usually only occurs if your code supplies an event helper like `mqtt.async_subscribe` or `track_state_change_listener`.
91+
92+
To help with this, there are two helper methods on the hass object that you can call from inside the event loop:
93+
94+
#### {% linkable_title hass.async_run_job %}
95+
96+
Use this method if the function should be called as soon as possible. This will call callbacks immediately, schedule coroutines for execution on the event loop and schedule other functions to be run inside the thread pool.
97+
98+
| Callback | Call immediately.
99+
| Coroutine | Schedule for execution on the event loop.
100+
| Other functions | Schedule for execution in the thread pool.
101+
102+
#### {% linkable_title hass.async_add_job %}
103+
104+
Use this method if the function should be called but not get priority over already scheduled calls.
105+
106+
| Callback | Schedule for execution on the event loop.
107+
| Coroutine | Schedule for execution on the event loop.
108+
| Other functions | Schedule for execution in the thread pool.
109+
110+
### [Next step: Miscellaneous &raquo;](/developers/asyncio_misc/)
111+
112+
[dev-docs]: https://dev-docs.home-assistant.io/en/master/api/core.html
113+
[dev-docs-async]: https://dev-docs.home-assistant.io/en/dev/api/util.html#module-homeassistant.util.async

0 commit comments

Comments
 (0)