Skip to content

Commit

Permalink
feat(util.tools): Wrapper for functions that need to be called in asy…
Browse files Browse the repository at this point in the history
…nc or sync context.
  • Loading branch information
tristpinsm committed Dec 4, 2024
1 parent 22c91d6 commit c2c8233
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
20 changes: 10 additions & 10 deletions python/pysmurf/client/tune/smurf_tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions python/pysmurf/client/util/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

0 comments on commit c2c8233

Please sign in to comment.