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 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)
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 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