@@ -118,6 +118,11 @@ def stop_trace(self):
118
118
119
119
120
120
def new_globals_file (filename ):
121
+ """Creates a new globals h5 file.
122
+
123
+ Creates a 'globals' group at the top level.
124
+ If file does not exist, a new h5 file is created.
125
+ """
121
126
with h5py .File (filename , 'w' ) as f :
122
127
f .create_group ('globals' )
123
128
@@ -132,7 +137,7 @@ def add_expansion_groups(filename):
132
137
requires_expansion_group = []
133
138
for groupname in f ['globals' ]:
134
139
group = f ['globals' ][groupname ]
135
- if not 'expansion' in group :
140
+ if 'expansion' not in group :
136
141
requires_expansion_group .append (groupname )
137
142
if requires_expansion_group :
138
143
group_globalslists = [get_globalslist (filename , groupname ) for groupname in requires_expansion_group ]
@@ -507,7 +512,7 @@ def evaluate_globals(sequence_globals, raise_exceptions=True):
507
512
for global_name in sequence_globals [group_name ]:
508
513
# Do not attempt to override exception objects already stored
509
514
# as the result of multiply defined globals:
510
- if not global_name in results [group_name ]:
515
+ if global_name not in results [group_name ]:
511
516
results [group_name ][global_name ] = evaled_globals [global_name ]
512
517
513
518
return results , global_hierarchy , expansions
@@ -810,33 +815,30 @@ def make_run_file_from_globals_files(labscript_file, globals_files, output_path,
810
815
make_single_run_file (output_path , sequence_globals , shots [0 ], sequence_attrs , 1 , 1 )
811
816
812
817
813
- def compile_labscript (labscript_file , run_file ):
814
- """Compiles labscript_file with the run file, returning
815
- the processes return code, stdout and stderr."""
816
- proc = subprocess .Popen ([sys .executable , labscript_file , run_file ], stdout = subprocess .PIPE , stderr = subprocess .PIPE )
817
- stdout , stderr = proc .communicate ()
818
- return proc .returncode , stdout , stderr
819
-
820
-
821
- def compile_labscript_with_globals_files (labscript_file , globals_files , output_path ):
822
- """Creates a run file output_path, using all the globals from
823
- globals_files. Compiles labscript_file with the run file, returning
824
- the processes return code, stdout and stderr."""
825
- make_run_file_from_globals_files (labscript_file , globals_files , output_path )
826
- returncode , stdout , stderr = compile_labscript (labscript_file , output_path )
827
- return returncode , stdout , stderr
828
-
829
-
830
- def compile_labscript_async (labscript_file , run_file , stream_port , done_callback ):
831
- """Compiles labscript_file with run_file. This function is designed to be called in
832
- a thread. The stdout and stderr from the compilation will be shovelled into
818
+ def compile_labscript_async (labscript_file , run_file ,
819
+ stream_port = None , done_callback = None ):
820
+ """Compiles labscript_file with run_file.
821
+
822
+ This function is designed to be called in a thread.
823
+ The stdout and stderr from the compilation will be shovelled into
833
824
stream_port via zmq push as it spews forth, and when compilation is complete,
834
825
done_callback will be called with a boolean argument indicating success. Note that
835
826
the zmq communication will be encrypted, or not, according to security settings in
836
827
labconfig. If you want to receive the data on a zmq socket, do so using a PULL
837
828
socket created from a labscript_utils.ls_zprocess.Context, or using a
838
829
labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be configured
839
830
with the appropriate security settings and will be able to receive the messages.
831
+
832
+ Args:
833
+ labscript_file (str): Path to labscript file to be compiled
834
+ run_file (str): Path to h5 file where compilation output is stored.
835
+ This file must already exist with proper globals initialization.
836
+ See :func:`new_globals_file` for details.
837
+ stream_port (zmq.socket, optional): ZMQ socket to push stdout and stderr.
838
+ If None, defaults to calling process stdout/stderr. Default is None.
839
+ done_callback (function, optional): Callback function run when compilation finishes.
840
+ Takes a single boolean argument marking compilation success or failure.
841
+ If None, callback is skipped. Default is None.
840
842
"""
841
843
compiler_path = os .path .join (os .path .dirname (__file__ ), 'batch_compiler.py' )
842
844
to_child , from_child , child = process_tree .subprocess (
@@ -849,22 +851,37 @@ def compile_labscript_async(labscript_file, run_file, stream_port, done_callback
849
851
success = data
850
852
to_child .put (['quit' , None ])
851
853
child .communicate ()
852
- done_callback (success )
854
+ if done_callback is not None :
855
+ done_callback (success )
853
856
break
854
857
else :
855
858
raise RuntimeError ((signal , data ))
856
859
857
860
858
- def compile_multishot_async (labscript_file , run_files , stream_port , done_callback ):
859
- """Compiles labscript_file with run_files. This function is designed to be called in
860
- a thread. The stdout and stderr from the compilation will be shovelled into
861
+ def compile_multishot_async (labscript_file , run_files ,
862
+ stream_port = None , done_callback = None ):
863
+ """Compiles labscript_file with multiple run_files (ie globals).
864
+
865
+ This function is designed to be called in a thread.
866
+ The stdout and stderr from the compilation will be shovelled into
861
867
stream_port via zmq push as it spews forth, and when each compilation is complete,
862
868
done_callback will be called with a boolean argument indicating success. Compilation
863
869
will stop after the first failure. If you want to receive the data on a zmq socket,
864
870
do so using a PULL socket created from a labscript_utils.ls_zprocess.Context, or
865
871
using a labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be
866
872
configured with the appropriate security settings and will be able to receive the
867
- messages."""
873
+ messages.
874
+
875
+ Args:
876
+ labscript_file (str): Path to labscript file to be compiled
877
+ run_files (list of str): Paths to h5 file where compilation output is stored.
878
+ These files must already exist with proper globals initialization.
879
+ stream_port (zmq.socket, optional): ZMQ socket to push stdout and stderr.
880
+ If None, defaults to calling process stdout/stderr. Default is None.
881
+ done_callback (function, optional): Callback function run when compilation finishes.
882
+ Takes a single boolean argument marking compilation success or failure.
883
+ If None, callback is skipped. Default is None.
884
+ """
868
885
compiler_path = os .path .join (os .path .dirname (__file__ ), 'batch_compiler.py' )
869
886
to_child , from_child , child = process_tree .subprocess (
870
887
compiler_path , output_redirection_port = stream_port
@@ -876,7 +893,8 @@ def compile_multishot_async(labscript_file, run_files, stream_port, done_callbac
876
893
signal , data = from_child .get ()
877
894
if signal == 'done' :
878
895
success = data
879
- done_callback (data )
896
+ if done_callback is not None :
897
+ done_callback (data )
880
898
break
881
899
if not success :
882
900
break
@@ -890,16 +908,28 @@ def compile_multishot_async(labscript_file, run_files, stream_port, done_callbac
890
908
child .communicate ()
891
909
892
910
893
- def compile_labscript_with_globals_files_async (labscript_file , globals_files , output_path , stream_port , done_callback ):
894
- """Same as compile_labscript_with_globals_files, except it launches a thread to do
895
- the work and does not return anything. Instead, stderr and stdout will be put to
911
+ def compile_labscript_with_globals_files_async (labscript_file , globals_files , output_path ,
912
+ stream_port , done_callback ):
913
+ """Compiles labscript_file with multiple globals files into a directory.
914
+
915
+ Instead, stderr and stdout will be put to
896
916
stream_port via zmq push in the multipart message format ['stdout','hello, world\n ']
897
917
etc. When compilation is finished, the function done_callback will be called a
898
918
boolean argument indicating success or failure. If you want to receive the data on
899
919
a zmq socket, do so using a PULL socket created from a
900
920
labscript_utils.ls_zprocess.Context, or using a
901
921
labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be configured with
902
- the appropriate security settings and will be able to receive the messages."""
922
+ the appropriate security settings and will be able to receive the messages.
923
+
924
+ Args:
925
+ labscript_file (str): Path to labscript file to be compiled
926
+ globals_files (list of str): Paths to h5 file where globals values to be used are stored.
927
+ See :func:`make_run_file_from_globals_files` for details.
928
+ output_path (str): Folder to save compiled h5 files to.
929
+ stream_port (zmq.socket): ZMQ socket to push stdout and stderr.
930
+ done_callback (function): Callback function run when compilation finishes.
931
+ Takes a single boolean argument marking compilation success or failure.
932
+ """
903
933
try :
904
934
make_run_file_from_globals_files (labscript_file , globals_files , output_path )
905
935
thread = threading .Thread (
0 commit comments