mirror of
https://github.com/home-assistant/developers.home-assistant.git
synced 2025-07-29 12:16:30 +00:00
Add asyncio thread safety docs
This commit is contained in:
parent
351c42edcb
commit
aea468c8aa
17
docs/asyncio_thread_safety.md
Normal file
17
docs/asyncio_thread_safety.md
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
title: "Thread Safety with asyncio"
|
||||
---
|
||||
|
||||
Developing with asyncio requires careful attention to thread safety, as nearly all asyncio objects are not thread-safe. If you are just getting started with asyncio, review Python's documentation on [Developing with asyncio](https://docs.python.org/3/library/asyncio-dev.html) for tips to avoid pitfalls.
|
||||
|
||||
Home Assistant has its conventions for deciding how to run a function based on whether it is decorated with `@callback`; for more details, see [Working with Async](asyncio_working_with_async.md).
|
||||
|
||||
Be sure to enable [`asyncio` debug mode](https://docs.python.org/3/library/asyncio-dev.html#debug-mode) and [Home Assistant's built-in debug mode](https://www.home-assistant.io/integrations/homeassistant/#debug) during development as many thread safety errors can be detected automatically.
|
||||
|
||||
## Solving thread safety errors
|
||||
|
||||
You may have reached this page because Home Assistant detected and reported a thread safety error. Beginning in version 2024.5.0, Home Assistant can detect, report, and block some non-threaded operations to prevent system instability. Before the Home Assistant could detect these errors, they may have led to unexpected restarts or undefined behaviors, as they can corrupt the internal asyncio state. Below are some tips on how to correct non-threaded operations.
|
||||
|
||||
### hass.async_create_task
|
||||
|
||||
`hass.async_create_task` should only be called from the event loop thread. When creating a task from another thread, instead use `hass.create_task`
|
@ -285,6 +285,7 @@ module.exports = {
|
||||
"asyncio_101",
|
||||
"asyncio_categorizing_functions",
|
||||
"asyncio_working_with_async",
|
||||
"asyncio_thread_safety",
|
||||
],
|
||||
},
|
||||
],
|
||||
|
Loading…
x
Reference in New Issue
Block a user