Skip to content

Commit 3eac923

Browse files
Merged in zprocess_changes (pull request labscript-suite#28)
Update to use new zprocess functionality
2 parents da516d7 + c5be3bd commit 3eac923

File tree

3 files changed

+68
-38
lines changed

3 files changed

+68
-38
lines changed

__init__.py

+44-22
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
if PY2:
1717
str = unicode
1818

19+
try:
20+
from labscript_utils import check_version
21+
except ImportError:
22+
raise ImportError('Require labscript_utils > 2.1.0')
23+
1924
import itertools
2025
import os
2126
import sys
@@ -30,7 +35,9 @@
3035
import h5py
3136
import numpy as np
3237

33-
import zprocess
38+
check_version('labscript_utils', '2.11.0', '3')
39+
from labscript_utils.ls_zprocess import ProcessTree, zmq_push_multipart
40+
process_tree = ProcessTree.instance()
3441

3542
__version__ = '2.2.0'
3643

@@ -681,13 +688,20 @@ def compile_labscript_with_globals_files(labscript_file, globals_files, output_p
681688

682689

683690
def compile_labscript_async(labscript_file, run_file, stream_port, done_callback):
684-
"""Compiles labscript_file with run_file. This function is designed
685-
to be called in a thread. The stdout and stderr from the compilation
686-
will be shoveled into stream_port via zmq push as it spews forth, and
687-
when compilation is complete, done_callback will be called with a
688-
boolean argument indicating success."""
691+
"""Compiles labscript_file with run_file. This function is designed to be called in
692+
a thread. The stdout and stderr from the compilation will be shovelled into
693+
stream_port via zmq push as it spews forth, and when compilation is complete,
694+
done_callback will be called with a boolean argument indicating success. Note that
695+
the zmq communication will be encrypted, or not, according to security settings in
696+
labconfig. If you want to receive the data on a zmq socket, do so using a PULL
697+
socket created from a labscript_utils.ls_zprocess.Context, or using a
698+
labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be configured
699+
with the appropriate security settings and will be able to receive the messages.
700+
"""
689701
compiler_path = os.path.join(os.path.dirname(__file__), 'batch_compiler.py')
690-
to_child, from_child, child = zprocess.subprocess_with_queues(compiler_path, stream_port)
702+
to_child, from_child, child = process_tree.subprocess(
703+
compiler_path, output_redirection_port=stream_port
704+
)
691705
to_child.put(['compile', [labscript_file, run_file]])
692706
while True:
693707
signal, data = from_child.get()
@@ -702,14 +716,19 @@ def compile_labscript_async(labscript_file, run_file, stream_port, done_callback
702716

703717

704718
def compile_multishot_async(labscript_file, run_files, stream_port, done_callback):
705-
"""Compiles labscript_file with run_files. This function is designed
706-
to be called in a thread. The stdout and stderr from the compilation
707-
will be shoveled into stream_port via zmq push as it spews forth,
708-
and when each compilation is complete, done_callback will be called
709-
with a boolean argument indicating success. Compilation will stop
710-
after the first failure."""
719+
"""Compiles labscript_file with run_files. This function is designed to be called in
720+
a thread. The stdout and stderr from the compilation will be shovelled into
721+
stream_port via zmq push as it spews forth, and when each compilation is complete,
722+
done_callback will be called with a boolean argument indicating success. Compilation
723+
will stop after the first failure. If you want to receive the data on a zmq socket,
724+
do so using a PULL socket created from a labscript_utils.ls_zprocess.Context, or
725+
using a labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be
726+
configured with the appropriate security settings and will be able to receive the
727+
messages."""
711728
compiler_path = os.path.join(os.path.dirname(__file__), 'batch_compiler.py')
712-
to_child, from_child, child = zprocess.subprocess_with_queues(compiler_path, stream_port)
729+
to_child, from_child, child = process_tree.subprocess(
730+
compiler_path, output_redirection_port=stream_port
731+
)
713732
try:
714733
for run_file in run_files:
715734
to_child.put(['compile', [labscript_file, run_file]])
@@ -723,7 +742,7 @@ def compile_multishot_async(labscript_file, run_files, stream_port, done_callbac
723742
break
724743
except Exception:
725744
error = traceback.format_exc()
726-
zprocess.zmq_push_multipart(stream_port, data=[b'stderr', error.encode('utf-8')])
745+
zmq_push_multipart(stream_port, data=[b'stderr', error.encode('utf-8')])
727746
to_child.put(['quit', None])
728747
child.communicate()
729748
raise
@@ -732,12 +751,15 @@ def compile_multishot_async(labscript_file, run_files, stream_port, done_callbac
732751

733752

734753
def compile_labscript_with_globals_files_async(labscript_file, globals_files, output_path, stream_port, done_callback):
735-
"""Same as compile_labscript_with_globals_files, except it launches
736-
a thread to do the work and does not return anything. Instead,
737-
stderr and stdout will be put to stream_port via zmq push in
738-
the multipart message format ['stdout','hello, world\n'] etc. When
739-
compilation is finished, the function done_callback will be called
740-
a boolean argument indicating success or failure."""
754+
"""Same as compile_labscript_with_globals_files, except it launches a thread to do
755+
the work and does not return anything. Instead, stderr and stdout will be put to
756+
stream_port via zmq push in the multipart message format ['stdout','hello, world\n']
757+
etc. When compilation is finished, the function done_callback will be called a
758+
boolean argument indicating success or failure. If you want to receive the data on
759+
a zmq socket, do so using a PULL socket created from a
760+
labscript_utils.ls_zprocess.Context, or using a
761+
labscript_utils.ls_zprocess.ZMQServer. These subclasses will also be configured with
762+
the appropriate security settings and will be able to receive the messages."""
741763
try:
742764
make_run_file_from_globals_files(labscript_file, globals_files, output_path)
743765
thread = threading.Thread(
@@ -746,7 +768,7 @@ def compile_labscript_with_globals_files_async(labscript_file, globals_files, ou
746768
thread.start()
747769
except Exception:
748770
error = traceback.format_exc()
749-
zprocess.zmq_push_multipart(stream_port, data=[b'stderr', error.encode('utf-8')])
771+
zmq_push_multipart(stream_port, data=[b'stderr', error.encode('utf-8')])
750772
t = threading.Thread(target=done_callback, args=(False,))
751773
t.daemon = True
752774
t.start()

__main__.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,17 @@
5858
splash.update_text('importing Qt')
5959
check_version('qtutils', '2.0.0', '3.0.0')
6060

61-
splash.update_text('importing zprocess')
62-
check_version('zprocess', '1.1.5', '3.0')
63-
6461
splash.update_text('importing pandas')
6562
check_version('pandas', '0.13', '2')
6663

6764
from qtutils.qt import QtCore, QtGui, QtWidgets
6865
from qtutils.qt.QtCore import pyqtSignal as Signal
6966

70-
import zprocess.locking
7167
from zmq import ZMQError
7268

7369
splash.update_text('importing labscript suite modules')
70+
check_version('labscript_utils', '2.11.0', '3')
71+
from labscript_utils.ls_zprocess import zmq_get, ProcessTree
7472
from labscript_utils.labconfig import LabConfig, config_prefix
7573
from labscript_utils.setup_logging import setup_logging
7674
import labscript_utils.shared_drive as shared_drive
@@ -84,8 +82,10 @@
8482
runmanager_dir = os.path.dirname(os.path.realpath(__file__))
8583
os.chdir(runmanager_dir)
8684

85+
process_tree = ProcessTree.instance()
86+
8787
# Set a meaningful name for zprocess.locking's client id:
88-
zprocess.locking.set_client_process_name('runmanager')
88+
process_tree.zlock_client.set_process_name('runmanager')
8989

9090

9191
def log_if_global(g, g_list, message):
@@ -1331,8 +1331,9 @@ def __init__(self):
13311331

13321332
splash.update_text('starting compiler subprocess')
13331333
# Start the compiler subprocess:
1334-
self.to_child, self.from_child, self.child = zprocess.subprocess_with_queues(
1335-
'batch_compiler.py', self.output_box.port)
1334+
self.to_child, self.from_child, self.child = process_tree.subprocess(
1335+
'batch_compiler.py', output_redirection_port=self.output_box.port
1336+
)
13361337

13371338
# Start a thread to monitor the time of day and create new shot output
13381339
# folders for each day:
@@ -1748,8 +1749,9 @@ def check_child_exited(self, timeout_time, kill=False):
17481749
else:
17491750
self.output_box.output('done.\n')
17501751
self.output_box.output('Spawning new compiler subprocess...')
1751-
self.to_child, self.from_child, self.child = zprocess.subprocess_with_queues(
1752-
'batch_compiler.py', self.output_box.port)
1752+
self.to_child, self.from_child, self.child = process_tree.subprocess(
1753+
'batch_compiler.py', output_redirection_port=self.output_box.port
1754+
)
17531755
self.output_box.output('done.\n')
17541756
self.output_box.output('Ready.\n\n')
17551757

@@ -3347,7 +3349,7 @@ def send_to_BLACS(self, run_file, BLACS_hostname):
33473349
agnostic_path = shared_drive.path_to_agnostic(run_file)
33483350
self.output_box.output('Submitting run file %s.\n' % os.path.basename(run_file))
33493351
try:
3350-
response = zprocess.zmq_get(port, BLACS_hostname, data=agnostic_path)
3352+
response = zmq_get(port, BLACS_hostname, data=agnostic_path)
33513353
if 'added successfully' in response:
33523354
self.output_box.output(response)
33533355
else:
@@ -3360,7 +3362,7 @@ def send_to_runviewer(self, run_file):
33603362
runviewer_port = int(self.exp_config.get('ports', 'runviewer'))
33613363
agnostic_path = shared_drive.path_to_agnostic(run_file)
33623364
try:
3363-
response = zprocess.zmq_get(runviewer_port, 'localhost', data='hello', timeout=1)
3365+
response = zmq_get(runviewer_port, 'localhost', data='hello', timeout=1)
33643366
if 'hello' not in response:
33653367
raise Exception(response)
33663368
except Exception as e:
@@ -3379,12 +3381,12 @@ def send_to_runviewer(self, run_file):
33793381
stdin=devnull, stdout=devnull, stderr=devnull, close_fds=True)
33803382
os._exit(0)
33813383
try:
3382-
zprocess.zmq_get(runviewer_port, 'localhost', data='hello', timeout=15)
3384+
zmq_get(runviewer_port, 'localhost', data='hello', timeout=15)
33833385
except Exception as e:
33843386
self.output_box.output('Couldn\'t submit shot to runviewer: %s\n\n' % str(e), red=True)
33853387

33863388
try:
3387-
response = zprocess.zmq_get(runviewer_port, 'localhost', data=agnostic_path, timeout=0.5)
3389+
response = zmq_get(runviewer_port, 'localhost', data=agnostic_path, timeout=0.5)
33883390
if 'ok' not in response:
33893391
raise Exception(response)
33903392
else:

batch_compiler.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
if PY2:
1616
str = unicode
1717

18+
from labscript_utils.ls_zprocess import ProcessTree
19+
process_tree = ProcessTree.connect_to_parent()
20+
to_parent = process_tree.to_parent
21+
from_parent = process_tree.from_parent
22+
kill_lock = process_tree.kill_lock
23+
24+
# Set a meaningful name for zprocess.locking's client id:
25+
process_tree.zlock_client.set_process_name('runmanager.batch_compiler')
26+
1827
import os
1928
import sys
2029
import traceback
21-
from zprocess import setup_connection_with_parent
22-
to_parent, from_parent, kill_lock = setup_connection_with_parent(lock = True)
2330

2431
import labscript
25-
import labscript_utils.excepthook
2632
from labscript_utils.modulewatcher import ModuleWatcher
2733

2834
class BatchProcessor(object):

0 commit comments

Comments
 (0)