def add_task(): count = request.args.get('count', default=1, type=int) print(count) response = [] for i in range(count): r = add.delay(1, 2) response.append({'task_id': r.task_id, 'task': '1 + 2 = ?'}) return jsonify(response)
def index(): a = random.randint(1, 100) b = random.randint(1, 100) started = time.time() task = add.delay(a, b) current_app.logger.info("task.id = {}".format(task.id)) answer = task.get() elapsed = time.time() - started bindings = dict( a=a, b=b, task_id=task.id, elapsed=elapsed, answer=answer, ) return render_template('index.html', **bindings)
def test_send_task(x=100, y=200): """ 后台发送任务测试 http://localhost:8000/test_send_task/?x=100&y=200 """ from app.tasks import add, mul, xsum x = int(request.args.get('x', x)) y = int(request.args.get('y', y)) add_res = add.delay(x, y) mul_res = mul.delay(x, y) xsum_res = xsum.delay([x, y]) result = '' result += '<a href="http://localhost:8000/test_task_add_result/%s/">%s</a><br/>' % ( add_res.id, add_res.id) result += '<a href="http://localhost:8000/test_task_mul_result/%s/">%s</a><br/>' % ( mul_res.id, mul_res.id) result += '<a href="http://localhost:8000/test_task_xsum_result/%s/">%s</a><br/>' % ( xsum_res.id, xsum_res.id) return result
def test_task_send_get(x=100, y=200): """ 后台发送任务并获取结果测试 http://localhost:8000/test_task_send_get/?x=100&y=200 """ from app.tasks import add, mul, xsum x = int(request.args.get('x', x)) y = int(request.args.get('y', y)) add_res = add.delay(x, y) mul_res = mul.delay(x, y) xsum_res = xsum.delay([x, y]) import time time.sleep(0.0005) result = { 'add_res': add_res.get(timeout=1.0) if add_res.state == 'SUCCESS' else '...', 'mul_res': mul_res.get(timeout=1.0) if mul_res.state == 'SUCCESS' else '...', 'xsum_res': xsum_res.get(timeout=1.0) if xsum_res.state == 'SUCCESS' else '...' } return json.dumps(result)
def index(): add.delay(2, 3) return render_template('index.html')
def test(request): for i in range(10000): add.delay(0, i) return HttpResponse("计算完毕")
def run_task(): r = add.delay(1, 2) r.wait() print(r.result) response = {'task_id': r.task_id, 'task': '1 + 2 = {}'.format(r.result)} return jsonify(response)
def test_add_task(self): result = add.delay('1', '2') self.assertTrue(result.successful()) self.assertEqual(result.result, '12', "Should return 12 on add string 1 and 2")
def add_view(request): add.delay(2, 4) return HttpResponse('OK')