Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b64c683

Browse files
added short section on race conditions
1 parent e6c7c3b commit b64c683

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎docs/reference.rst‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,27 @@ If a required package version differs from the installed one, no change is made
17211721
HASS has a requirement that pyscript should not change. In that case a warning message will be
17221722
logged and the requirement will be skipped.
17231723

1724+
Tasks are Asynchronous
1725+
^^^^^^^^^^^^^^^^^^^^^^
1726+
1727+
Asynchronous tasks can create unexpected race conditions in your code.
1728+
1729+
All trigger decorators call the function as an asynchronous task when the trigger occurs. All tasks
1730+
you create explicitly are also asynchronous. This allows each function to run in parallel with
1731+
other tasks, and to yield control to other tasks and all other HASS activities potentially anywhere
1732+
in the function. However, if two closely-spaced triggers occur (or different functions have the
1733+
same trigger), although the second trigger will begin running after the first, there is no guarantee
1734+
that the first task will have completed (or even executed any statements before the second task
1735+
start running. Both trigger functions will be running asynchronously, and the order of execution of
1736+
code among the tasks is not guaranteed. The same is true if you start two tasks using ``task.create()``
1737+
without any delay: the code in the tasks could run in any order relative to each other.
1738+
1739+
If this is a problem for your application logic, various solutions including using ``asyncio.Lock``
1740+
or ``asyncio.Event``, using ``task.unique()`` to ensure only one task is running at a time, or using
1741+
``state_hold`` in the trigger arguments to ensure the trigger condition persists for some time before
1742+
triggering the function.
1743+
1744+
17241745
Trigger Closures
17251746
^^^^^^^^^^^^^^^^
17261747

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /