def get_task_args(self): "extracts task args from a request" try: options = json_decode(self.request.body) except (TypeError, ValueError): raise web.HTTPError(400) args = options.pop('args', []) kwargs = options.pop('kwargs', {}) if not isinstance(args, (list, tuple)): raise web.HTTPError(400, 'task args must be a list or tuple') return args, kwargs, options
def post(self, taskname): """ Apply tasks synchronously. Function returns when the task is finished **Example request**: .. sourcecode:: http POST /apply/examples.tasks.add/ HTTP/1.1 Accept: application/json Accept-Encoding: gzip, deflate, compress Content-Length: 16 Content-Type: application/json; charset=utf-8 Host: localhost:8888 User-Agent: HTTPie/0.8.0 { "args": [ 1, 2 ] } **Example response**: .. sourcecode:: http HTTP/1.1 200 OK Content-Length: 84 Content-Type: application/json; charset=UTF-8 Server: TornadoServer/3.2 { "result": 3, "state": "SUCCESS", "task-id": "2ce70595-a028-4e0d-b906-be2183fc6821" } :statuscode 200: no error :statuscode 400: invalid request :statuscode 404: unknown task """ try: task = self.application.celery_app.tasks[taskname] except KeyError: raise web.HTTPError(404, "Unknown task '%s'" % taskname) args, kwargs, options = self.get_task_args() timeout = options.pop('timeout', None) task_id = uuid() htimeout = None if timeout: htimeout = ioloop.IOLoop.instance().add_timeout( timedelta(seconds=timeout), partial(self.on_time, task_id)) task.apply_async(args=args, kwargs=kwargs, task_id=task_id, callback=partial(self.on_complete, htimeout), **options)
def select_related(self, *columns, **options): depth = options.pop('depth', None) if options: raise TypeError('Unexpected argument %r' % iter(options).next()) if depth not in (None, 1): raise TypeError('Depth can only be 1 or None currently') need_all = depth is None columns = list(columns) for idx, column in enumerate(columns): column = column.replace('__', '.') if '.' in column: need_all = True columns[idx] = column func = (need_all and joinedload_all or joinedload) return self.options(func(*columns))
def select_related(self, *columns, **options): depth = options.pop("depth", None) if options: raise TypeError("Unexpected argument %r" % iter(options).next()) if depth not in (None, 1): raise TypeError("Depth can only be 1 or None currently") need_all = depth is None columns = list(columns) for idx, column in enumerate(columns): column = column.replace("__", ".") if "." in column: need_all = True columns[idx] = column func = need_all and joinedload_all or joinedload return self.options(func(*columns))