Skip to content

Commit

Permalink
fix: make background loops work with get_data_set. Fixes microsoft#395
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcjohnson committed Nov 21, 2016
1 parent aee2aef commit 77c4766
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions qcodes/loops.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ def _check_signal(self):
else:
raise ValueError('unknown signal', signal_)

def get_data_set(self, data_manager=USE_MP, *args, **kwargs):
def get_data_set(self, *args, data_manager=None, quiet=False, **kwargs):
"""
Return the data set for this loop.
Expand All @@ -680,6 +680,7 @@ def get_data_set(self, data_manager=USE_MP, *args, **kwargs):
data_manager: a DataManager instance (omit to use default,
False to store locally)
quiet (bool): True to hide some messages. Default False.
kwargs are passed along to data_set.new_data. The key ones are:
location: the location of the DataSet, a string whose meaning
Expand All @@ -699,6 +700,15 @@ def get_data_set(self, data_manager=USE_MP, *args, **kwargs):
a DataSet object that we can use to plot
"""
if self.data_set is None:
# Before we make a new DataSet, ensure that there is no background
# loop running. This is only strictly necessary if we are using
# a DataManager, but if we're NOT we shouldn't have to worry about
# background loops anyway so lets keep it simple.
self.wait_for_bg(quiet)

if data_manager is None:
data_manager = USE_MP

if data_manager is False:
data_mode = DataMode.LOCAL
else:
Expand All @@ -714,7 +724,8 @@ def get_data_set(self, data_manager=USE_MP, *args, **kwargs):
else:
has_args = len(kwargs) or len(args)
uses_data_manager = (self.data_set.mode != DataMode.LOCAL)
if has_args or (uses_data_manager != data_manager):
if has_args or ((uses_data_manager != data_manager) and
(data_manager is not None)):
raise RuntimeError(
'The DataSet for this loop already exists. '
'You can only provide DataSet attributes, such as '
Expand All @@ -723,6 +734,18 @@ def get_data_set(self, data_manager=USE_MP, *args, **kwargs):

return self.data_set

def wait_for_bg(self, quiet):
prev_loop = get_bg()
if prev_loop:
if not quiet:
print('Waiting for the previous background Loop to finish...',
flush=True)
prev_loop.join()

if prev_loop and not quiet:
print('...done. Starting ' + (data_set.location or 'new loop'),
flush=True)

def run_temp(self, **kwargs):
"""
wrapper to run this loop in the foreground as a temporary data set,
Expand All @@ -733,7 +756,7 @@ def run_temp(self, **kwargs):
data_manager=False, location=False, **kwargs)

def run(self, background=USE_MP, use_threads=False, quiet=False,
data_manager=USE_MP, station=None, progress_interval=False,
data_manager=None, station=None, progress_interval=False,
*args, **kwargs):
"""
Execute this loop.
Expand All @@ -745,6 +768,8 @@ def run(self, background=USE_MP, use_threads=False, quiet=False,
parallel (as long as they don't block each other)
quiet: (default False): set True to not print anything except errors
data_manager: set to True to use a DataManager. Default to False.
If you previously set a data_manager in ``get_data_set``, you can
omit it here to use the setting provided previously.
station: a Station instance for snapshots (omit to use a previously
provided Station, or the default Station)
progress_interval (default None): show progress of the loop every x
Expand Down Expand Up @@ -776,16 +801,11 @@ def run(self, background=USE_MP, use_threads=False, quiet=False,
if progress_interval is not False:
self.progress_interval = progress_interval

prev_loop = get_bg()
if prev_loop:
if not quiet:
print('Waiting for the previous background Loop to finish...',
flush=True)
prev_loop.join()
data_set = self.get_data_set(data_manager, *args, quiet=quiet,
**kwargs)
data_manager = getattr(data_set, 'data_manager', False)

data_set = self.get_data_set(data_manager, *args, **kwargs)

if background and not getattr(data_set, 'data_manager', None):
if background and not data_manager:
warnings.warn(
'With background=True you must also set data_manager=True '
'or you will not be able to sync your DataSet.',
Expand All @@ -811,10 +831,6 @@ def run(self, background=USE_MP, use_threads=False, quiet=False,

data_set.save_metadata()

if prev_loop and not quiet:
print('...done. Starting ' + (data_set.location or 'new loop'),
flush=True)

try:
if background:
warnings.warn("Multiprocessing is in beta, use at own risk",
Expand Down Expand Up @@ -855,7 +871,6 @@ def run(self, background=USE_MP, use_threads=False, quiet=False,
# this one later.
self.data_set = None


return ds

def _compile_actions(self, actions, action_indices=()):
Expand Down

0 comments on commit 77c4766

Please sign in to comment.