Auto Refresh#
In daily operations, you may want to automatically refresh the lab when some codes are changed.
AlabOS provides a convinient way to do this by using the auto_refresh configuration.
To do so, just add the following line to your AlabOS configuration yaml file:
[general]
...
auto_refresh = true
By default, this feature is disabled. When enabled, AlabOS will monitor the source code files and automatically refresh the lab when any changes are detected.
How It Works#
Main AlabOS process (Device and Task Manager): When there is a change in the device or task definition, the device and task manager will be re-imported. It is a process with the following steps:
Task manager will stop to launch new tasks.
Resource manager will stop to allocate new resources.
Device manager will try to pause all the devices.
Wait for all the tasks are NOT in RUNNING state (no communication with the devices).
All the devices will be disconnected.
Device, sample positions (including the ones defined inside the devices), and task manager will be re-imported. For device and sample positions, differences will be automatically calculated and applied (by the device manager) once the devices and sample positions are unoccupied. However, the system will continue with the following steps as the waiting for the unoccupation is done in parallel to the system running time.
Connect to the devices again.
Device manager will try to resume all the devices.
Resource manager will resume to allocate new resources.
Task manager will resume to launch new tasks.
Task Actor: Task actor is the function that actually runs the task. At the beginning of each task process,
the device and task definition will be re-imported in all managers (task, device, and experiment managers). This means that any changes made to the task definition
or device code will be reflected in the task execution. If auto_refresh is not enabled or missing in the configuration,
the task actor will not be re-imported, and any changes made to the task definition or device
code will not be reflected in the task execution. The re-import is done through the lowest level first (modules .py files) up to the highest level (init.py in the working directory).
Note that the change will be reflected in the newly launched tasks right away, at the importing time. But for the device and task managers, it will check the file modification time every 30 s. This usually will not be a problem as the new task launching should typically take a while before it starts to send commands to the devices.
Limitations#
The auto_refresh feature has some limitations:
Refreshes cannot be done under multiple threads. This is due to the fact that the
auto_refreshfeature is not thread-safe (multiple definitions of the same module can occur). By default, thelaunch_workeruses 6 processes where each process usually use 140MB (total < 1 GB of memory) with 128 threads each. This is not a problem when NOT usingauto_refreshor the system refresh functionalities. However, when usingauto_refresh, thelaunch_workerwill default to running a single thread with 72 processes where each process usually use 140MB (total ~10 GB of memory), therefore only 72 tasks can be initiated and running in parallel. This can be overriden by launching thelaunch_workerwith a different number of processes and threads. However, if using more than one thread, keep in mind that imports of devices and tasks in tasks definitions have to be done in the.run()method and any other methods that uses those devices and tasks in the task class due to the nature ofrun_taskanddramatiq. Never import from the usual module level imports (top part of your file). See thefake_labstructure as reference, wherefrom .. import Furnace, Movingis done in.run()ofheating.pyandfrom .devices import ...andfrom .tasks import ...is done in the__init__.pyoffake_labfor an example.It only works for Python source code files. Changes to other types of files, such as configuration files or data files, will not trigger a refresh.
External libraries or dependencies that are not part of the AlabOS codebase will not be monitored for changes. If you modify such libraries, you will need to manually refresh the lab.
New devices and tasks: If you add new devices or tasks, you will need to manually refresh the lab or there may be some unexpected behaviors.
For the tasks that are already running, the changes will not be reflected until the task is refreshed. This means that if you change the device or task definition, the running tasks will continue to use the old definitions. Keep in mind that the best practice is to ensure devices and tasks definitions are backward compatible to the older versions so that the running tasks can continue to run without any issues and not have a conflict with the new definitions.
No user input should be done during the refresh process to avoid unexpected behaviors. No blocking is currently implemented.
No support for task removal. It will require further implementation in checking running tasks and submitted experiments for the task being removed. But this is less important as removing a task is not a common operation.
Tests#
The auto_refresh feature is tested by:
adding and removing slots on a standalone sample position/changing the numbers
adding and removing a device and its sample position
adding and removing whole sample position
adding and removing a sample position prefix in a device
changing the numbers in a sample position in a device
changing the definition of a task.
adding a new class of task.
adding a new class of device.