.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/example_bash_script.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_example_bash_script.py: Use a bash script ================= .. GENERATED FROM PYTHON SOURCE LINES 5-19 .. code-block:: Python from pathlib import Path import time import subprocess from remote_run.run import ( ExecutionContext, SlurmSchedulingEngine, GuixRunner, GitProject, remote, is_finished, SshExecution, ) .. GENERATED FROM PYTHON SOURCE LINES 20-22 Define an execution context with a scheduling engine. For slurm you can pass slurm parameters directly here. .. GENERATED FROM PYTHON SOURCE LINES 22-37 .. code-block:: Python execution_context = ExecutionContext( execution=SshExecution( machine="shpc0003.ost.ch", working_directory=Path("/cluster/raid/home/reza.housseini"), ), project=GitProject(), runner=GuixRunner(channels=Path("channels.scm").read_text()), scheduling_engine=SlurmSchedulingEngine( job_name="run_bash_script", mail_type="ALL", mail_user="reza.housseini@ost.ch", ), ) .. GENERATED FROM PYTHON SOURCE LINES 38-40 decorate your functions you want to run in the specified execution context .. GENERATED FROM PYTHON SOURCE LINES 40-47 .. code-block:: Python @remote(execution_context) def execute_bash_commands(message): return subprocess.run( ["echo", message, ";", "hostname"], capture_output=True, encoding="utf-8" ).stdout .. GENERATED FROM PYTHON SOURCE LINES 48-52 this call will run on the remote machine specified in execution_context but due to the asynchronous nature of scheduling engines this will not return the result, instead you get the job id and a function to retrieve the result later. .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python job_id, result_func = execute_bash_commands("hello") .. GENERATED FROM PYTHON SOURCE LINES 55-56 now we wait for the remote execution to finish before retrieving the result .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python time.sleep(20) .. GENERATED FROM PYTHON SOURCE LINES 59-60 we should check if the job id has finished before retrieving the result .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python if is_finished(execution_context, job_id): assert result_func() == "hello\nshpc0003" .. _sphx_glr_download_auto_examples_example_bash_script.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_bash_script.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_bash_script.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_bash_script.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_