@@ -242,8 +242,10 @@ built-in service (so you can't create your own service with that name). All func
242242services and triggers are re-created on `reload`. Any currently running functions (ie, functions
243243that have been triggered and are actively executing Python code or waiting inside `task.sleep()`
244244or `task.wait_until()`) are not stopped by `reload` - they continue to run until they finish
245- (return). You can terminate these running functions too on `reload` if you prefer by using
246- ` task.unique()` in both the script file preamble and inside those functions.
245+ (return). You can terminate these running functions too on `reload` if you wish by calling
246+ ` task.unique()` in the script file preamble (ie, outside any function definition), so
247+ it is executed on load and reload, which will terminate any running functions that have
248+ previously called `task.unique()` with the same argument.
247249
248250# # Accessing state variables
249251
@@ -623,23 +625,34 @@ will log all messages at `info` or higher (ie: `log.info()`, `log.warning()` and
623625inside `my_function` defined in the script file `my_scripts.py` (and any other functions it calls)
624626will log all messages at `debug` or higher.
625627
628+ Note that in Jupyter, all the `log` functions will display output in your session, independent of
629+ the `logger` configuration settings.
630+
626631# ### Sleep
627632
628633` task.sleep(seconds)` sleeps for the indicated number of seconds, which can be floating point. Do not
629634import `time` and use `time.sleep()` - that will block lots of other activity.
630635
631636# ### Task Unique
632637
633- ` task.unique(task_name, kill_me=False)` kills any running task that previously called `task.unique`
634- with the same `task_name`. The name can be any string. If `kill_me` is `True` then the current
635- task is killed if another task that is running previously called `task.unique` with the same
636- ` task_name` .
638+ ` task.unique(task_name, kill_me=False)` kills any currently running triggered function that
639+ previously called `task.unique` with the same `task_name`. The name can be any string. If
640+ ` kill_me=True ` then the current task is killed if another task that is running previously called
641+ ` task.unique ` with the same ` task_name`.
637642
638643Note that `task.unique` applies across all global contexts. It's up to you to use a convention
639644for `task_name` that avoids accidential collisions. For example, you could use a prefix of the
640645script file name, so that all `task_unique` calls in `FILENAME.py` use a `task_name` that
641646starts with `"FILENAME."`.
642647
648+ ` task.unique` can also be called outside a function, for example in the preamble of a script file or
649+ interactively using Jupyter. That causes any currently running functions (ie, functions that have
650+ already been triggered and are running Python code) that previously called `task.unique` with the
651+ same name to be terminated. Since any currently running functions are not terminated on reload, this
652+ is the mechanism you can use should you wish to terminate specific functions on reload. If used
653+ outside a function or interactively with Jupyter, calling `task.unique` with `kill_me=True` causes
654+ ` task.unique` to do nothing.
655+
643656# ### Waiting for events
644657
645658` task.wait_until()` allows functions to wait for events, using identical syntax to the decorators.
@@ -739,8 +752,8 @@ after `task.wait_until()` runs.
739752
740753# ### Global Context functions
741754
742- Each pyscript script file runs inside its own global context, which means their global variables and
743- functions are isolated from other script files . Each Jupyter session also runs in its own global
755+ Each pyscript script file runs inside its own global context, which means their global variables
756+ and functions are isolated from each other . Each Jupyter session also runs in its own global
744757context. For a script file called `FILENAME.py`, its global context is `file.FILENAME`. Each
745758Jupyter global context name is `jupyter_NNN` where `NNN` is a unique integer starting at 0.
746759
@@ -749,7 +762,8 @@ development, you might want your Jupyter session to access variables and functio
749762a script file. Three functions are provided for getting, setting and listing the global
750763contexts. That allows you to interactively change the global context during a Jupyter
751764session. You could also use these functions in your script files, but that is strongly
752- discouraged. Here are the functions :
765+ discouraged because it violates the name space isolation among the script files.
766+ Here are the functions :
753767- ` pyscript.get_global_ctx()` returns the current global context name.
754768- ` pyscript.list_global_ctx()` lists all the global contexts, with the current global context listed first.
755769- ` pyscript.set_global_ctx(new_ctx_name)` sets the current global context to the given name.
@@ -759,7 +773,7 @@ services and variables you created are deleted (HASS state variables survive). I
759773a script file's context, then any triggers, functions, services or variables you interactively
760774create there will persist after you exit the Jupyter session. However, if you don't update the
761775corresponding script file, then upon the next pyscript reload or HASS restart, those interactive
762- changes will be lost.
776+ changes will be lost, since reloading a script file recreates a new global context .
763777
764778# # Contributing
765779
0 commit comments