@@ -1721,6 +1721,27 @@ If a required package version differs from the installed one, no change is made
1721
1721
HASS has a requirement that pyscript should not change. In that case a warning message will be
1722
1722
logged and the requirement will be skipped.
1723
1723
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
+
1724
1745
Trigger Closures
1725
1746
^^^^^^^^^^^^^^^^
1726
1747
0 commit comments