class GConnection(Async): def __init__(self, *args, **kwargs): """ This class is a 'GEvent'-optimized subclass of libcouchbase which utilizes the underlying IOPS structures and the gevent event primitives to efficiently utilize couroutine switching. """ super(GConnection, self).__init__(IOPS(), *args, **kwargs) def _do_ctor_connect(self): if self.connected: return self._connect() self._evconn = AsyncResult() self._conncb = self._on_connected self._evconn.get() self._evconn = None def _on_connected(self, err): if err: self._evconn.set_exception(err) else: self._evconn.set(None) def _waitwrap(self, cbasync): cur_thread = getcurrent() cbasync.callback = cur_thread.switch cbasync.errback = lambda r, x, y, z: cur_thread.throw(x, y, z) return get_hub().switch() def _meth_factory(meth, name): def ret(self, *args, **kwargs): return self._waitwrap(meth(self, *args, **kwargs)) return ret def _http_request(self, **kwargs): res = super(GConnection, self)._http_request(**kwargs) if kwargs.get('chunked', False): return res #views e = Event() res._callback = lambda x, y: e.set() e.wait() res._maybe_raise() return res def query(self, *args, **kwargs): kwargs['itercls'] = GView ret = super(GConnection, self).query(*args, **kwargs) ret.start() return ret locals().update(Async._gen_memd_wrappers(_meth_factory))
def process_async_task(headers, request_body): """Process an Async task and execute the requested function.""" async_options = json.loads(request_body) work = Async.from_dict(async_options) logging.info(work._function_path) run_job(work) return 200, work._function_path
def ProgressAsync(function): async = Async(function) @functools.wraps(function) def progress_async(*args, **keys): progress = ProgressFuture() keys['report'] = progress.OnReport progress.Future = ProgressFuture(async (*args, **keys)) return progress return progress_async
def run_task(task_fn, *args, **kwargs): """Run `task_fn` in a separate thread. Return an Async model that will hold its result upon completion.""" async_task = Async() async_task.save() task = RunningTask() task.async_id = async_task.id task.thread = threading.Thread( target=AsyncManager._wrap_task(task, task_fn), args=args, kwargs=kwargs ) # Note: Important to start the thread prior to adding it to our list of # running tasks. Otherwise the is_alive check might fire before the # thread is started and mark it as completed prematurely. task.thread.start() with AsyncManager.lock: AsyncManager.running_tasks.append(task) return async_task
def decode_callbacks(encoded_callbacks): """Decode the callbacks to an executable form.""" from .async import Async callbacks = {} for event, callback in encoded_callbacks.iteritems(): if isinstance(callback, dict): callback = Async.from_dict(callback) else: callback = path_to_reference(callback) callbacks[event] = callback return callbacks
def add(self, target, args=None, kwargs=None, **options): """Add an Async job to this context. Takes an Async object or the argumnets to construct an Async object as argumnets. Returns the newly added Async object. """ from .. async import Async from ..batcher import Message if self._tasks_inserted: raise errors.ContextAlreadyStartedError( "This Context has already had its tasks inserted.") if not isinstance(target, (Async, Message)): target = Async(target, args, kwargs, **options) self._tasks.append(target) return target
def polling(self): try: new_offset = None while True: req = self.method("getUpdates", { 'offset': new_offset, 'timeout': 30, 'limit': 40 }) if not 'result' in req: continue if len(req['result']) == 0: continue #print(req) new_offset = int(req["result"][-1]['update_id']) + 1 loop = Async() for event in req['result']: if 'callback_query' in event: if 'inline_query' in event['callback_query']: event = InlineQuery( event['callback_query']['inline_query'], self) loop.addTask(event) continue event = CallBackQuery(event['callback_query'], self) loop.addTask(self.callbackHandler(event)) continue elif 'inline_query' in event: event = InlineQuery(event['inline_query'], self) loop.addTask(self.inlineHandler(event)) continue event = Message(event, self) textString = str(event.chat_id) + "_" + str( event.user['id']) if textString in self.nextMessages: print("in") if self.nextMessages[textString]['count'] == 0: event.params = self.nextMessages[textString][ 'params'] loop.addTask(self.nextMessages[textString] ['handler'](event)) self.nextMessages[textString]['count'] = 1 del self.nextMessages[textString] continue if event == None: continue loop.addTask(self.handler(event)) loop.run() except KeyboardInterrupt: sys.exit(0)