Writing scripts
Since the behaviour of the Pioreactor is controlled by Python objects, you can write Python scripts that use those objects. Here's a simple example of starting the stirring by creating the Stirrer
object:
from pioreactor.background_jobs.stirring import Stirrer, RpmFromFrequency
from pioreactor.whoami import get_unit_name
from pioreactor.whoami import get_assigned_experiment_name
unit = get_unit_name()
experiment = get_assigned_experiment_name(unit)
st = Stirrer(
target_rpm=300,
unit=unit,
experiment=experiment,
rpm_calculator=RpmFromFrequency()
)
st.start_stirring()
st.block_until_disconnected() # pauses the execution, but stirring continues
Save this code to a local file on your Pioreactor's Raspberry Pi called stirring_script.py
. Then, running python stirring_scripy.py
, you should see that stirring on the Pioreactor starts. With the script running, you should also updates on the Pioreactor UI (ex: see pioreactor.local/pioreactors page). Typing ctrl-c
will exit the script.
What is get_unit_name
and get_assigned_experiment_name
? These are helper functions that get the current name of the Pioreactor, and the current experiment name, respectively. Using the current experiment name will ensure that your data shows up in the UI, and is correctly stored in the database.
Starting a long-running script
On the command line, you can run your script with
python your_script.py
If you want to run the script in the background (so you can close terminal and the job continues in the background), use
python your_script.py >/dev/null 2>&1 & disown
You can also use the Pioreactor's plugin architecture to control the start and stop of the script.
Useful utility objects
- RepeatedTimer: this class allows you to scheduale a function to run every N seconds in a non-blocking manner.