def gif(**kwargs): # does it need **kwargs? if example_mode: return send_from_directory(DATA_DIR, "forecast.gif") with conn.lock('gif_swap'): with open(miner.gif_storage.path("forecast.gif")) as f: sent_gif = cStringIO.StringIO(f.read()) return send_file(sent_gif, mimetype="image/gif")
def __init__(self, tempdir): """Check that tempdir exists and is empty.""" self.tempdir = tempdir #self.lock = threading.RLock() with conn.lock(self.tempdir): try: makedirs(self.tempdir) except OSError as exception: if exception.errno != errno.EEXIST: raise self.remove_all_files()
def _caller(*args, **kwargs): """Caller.""" return_value = None have_lock = False lock = conn.lock(key, timeout=timeout) try: if logger: logger.info('Trying to acquire {} lock.'.format(key)) have_lock = lock.acquire(blocking=blocking, blocking_timeout=blocking_timeout) if have_lock: if logger: logger.info('Got {} lock.'.format(key)) return_value = run_func(*args, **kwargs) elif logger: logger.info( 'Did not acquire lock. The {} is busy.'.format(key)) finally: if have_lock: lock.release() return return_value
def forecast(lon, lat): if example_mode: return generate_example_data() x, y = raster.lonlat_to_xy(lon, lat) forecasts = [] with conn.lock('temp_swap'): temp = miner.current_temp() for filename in temp.filenames(): with rasterio.open(temp.path(filename)) as data: mm_h = raster.rr_at_coords(data, x, y) timestamp = raster.filename_to_datestring(filename) forecasts.append({"time": timestamp, "rain_intensity": mm_h}) return json.dumps({ "time_format": fmi.TIME_FORMAT, "timezone": str(pytz.UTC), "forecasts": forecasts, "accumulation": raster.accumulation(map(lambda x: x['rain_intensity'], forecasts), 5), })
def filenames(self): """Returns names of the stored files sorted as newest to oldest""" with conn.lock(self.tempdir): filenames = listdir(self.tempdir) filenames.sort() return filenames
def remove_all_files(self): with conn.lock(self.tempdir): for filename in listdir(self.tempdir): remove(path.join(self.tempdir, filename))
def save_gif(self, png_paths): with conn.lock('gif_swap'): self.gif_storage.remove_all_files() vis.pngs2gif(png_paths, self.gif_storage.path('forecast.gif'))
def swap_temps(self): with conn.lock('temp_swap'): self.tempidx = (self.tempidx + 1) % len(self.temps)