diff --git a/python/pysmurf/client/tune/smurf_tune.py b/python/pysmurf/client/tune/smurf_tune.py index 41fc3683..402bb921 100644 --- a/python/pysmurf/client/tune/smurf_tune.py +++ b/python/pysmurf/client/tune/smurf_tune.py @@ -3476,6 +3476,7 @@ def check_lock_flux_ramp_off(self, band,df_max=.03, make_plot=make_plot, flux_ramp=False, **kwargs) @set_action() + @tools.async_or_sync def find_freq(self, band, start_freq=-250, stop_freq=250, subband=None, tone_power=None, n_read=2, make_plot=False, save_plot=True, plotname_append='', window=50, rolling_med=True, @@ -3528,15 +3529,13 @@ def find_freq(self, band, start_freq=-250, stop_freq=250, subband=None, min_gap : int, optional, default 2 Minimum number of samples between resonances. ''' - return asyncio.run( - self._async_find_freq( - band, start_freq=-250, stop_freq=250, subband=None, - tone_power=None, n_read=2, make_plot=False, save_plot=True, - plotname_append='', window=50, rolling_med=True, - make_subband_plot=False, show_plot=False, grad_cut=.05, - flip_phase=False, grad_kernel_width=8, - amp_cut=.25, pad=2, min_gap=2 - ) + return self._async_find_freq( + band, start_freq=-250, stop_freq=250, subband=None, + tone_power=None, n_read=2, make_plot=False, save_plot=True, + plotname_append='', window=50, rolling_med=True, + make_subband_plot=False, show_plot=False, grad_cut=.05, + flip_phase=False, grad_kernel_width=8, + amp_cut=.25, pad=2, min_gap=2 ) def parallel_find_freq(self, bands, *args, **kwargs): @@ -3714,8 +3713,9 @@ def plot_find_freq(self, f=None, resp=None, subband=None, filename=None, plt.close() @set_action() + @tools.async_or_sync def full_band_ampl_sweep(self, band, subband, tone_power, n_step=31): - return asyncio.run(self._async_full_band_ampl_sweep(band, subband, tone_power, n_step)) + return self._async_full_band_ampl_sweep(band, subband, tone_power, n_step) async def _async_full_band_ampl_sweep(self, band, subband, tone_power, n_step=31): """sweep a full band in amplitude, for finding frequencies diff --git a/python/pysmurf/client/util/tools.py b/python/pysmurf/client/util/tools.py index 96d4db8a..7ed1c298 100644 --- a/python/pysmurf/client/util/tools.py +++ b/python/pysmurf/client/util/tools.py @@ -15,6 +15,8 @@ #----------------------------------------------------------------------------- import numpy as np from scipy.optimize import curve_fit +import functools +import asyncio def skewed_lorentzian(x, bkg, bkg_slp, skw, mintrans, res_f, Q): """ Skewed Lorentzian model. @@ -232,3 +234,24 @@ def utf8_to_str(d): The string associated with input d. """ return ''.join([str(s, encoding='UTF-8') for s in d]) + + +def async_or_sync(func): + """ + Decorator for a functions that wraps asynchronous code. + We need to check if an asyncio event loop is running and + create one if not. + """ + @functools.wraps(func) + def wrapper(*args, **kwargs): + try: + loop = asyncio.get_running_loop() + if loop.is_running(): + # just return the coroutine + return func(*args, **kwargs) + else: + raise RuntimeError("Event loop is not running") + except RuntimeError: + # no event loop is running, create one + return asyncio.run(func(*args, **kwargs)) + return wrapper