def wrapper(*args, **kwargs): if vaex.cache.is_on(): key = key_function() value = vaex.cache.get(key, type=type) if value is None: t0 = time.time() if delay: promise = f(*args, **kwargs) def set(value): duration_wallclock = time.time() - t0 vaex.cache.set(key, value, type='computed', duration_wallclock=duration_wallclock) return value return promise.then(set) else: value = f(*args, **kwargs) duration_wallclock = time.time() - t0 vaex.cache.set(key, value, type='computed', duration_wallclock=duration_wallclock) return value else: if delay: return Promise.fulfilled(value) else: return value else: return f(*args, **kwargs)
def wrap_future_with_promise(future): from vaex.promise import Promise if isinstance(future, Promise): # TODO: not so nice, sometimes we pass a promise return future promise = Promise() def callback(future): e = future.exception() if e: promise.reject(e) else: promise.fulfill(future.result()) future.add_done_callback(callback) return promise