本文整理汇总了Python中celery.result.AsyncResult方法的典型用法代码示例。如果您正苦于以下问题:Python result.AsyncResult方法的具体用法?Python result.AsyncResult怎么用?Python result.AsyncResult使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类celery.result
的用法示例。
在下文中一共展示了result.AsyncResult方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch_celery_task_state
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def fetch_celery_task_state(async_result: AsyncResult) -> \
Tuple[str, Union[str, ExceptionWithTraceback], Any]:
"""
Fetch and return the state of the given celery task. The scope of this function is
global so that it can be called by subprocesses in the pool.
:param async_result: a tuple of the Celery task key and the async Celery object used
to fetch the task's state
:type async_result: tuple(str, celery.result.AsyncResult)
:return: a tuple of the Celery task key and the Celery state and the celery info
of the task
:rtype: tuple[str, str, str]
"""
try:
with timeout(seconds=OPERATION_TIMEOUT):
# Accessing state property of celery task will make actual network request
# to get the current state of the task
info = async_result.info if hasattr(async_result, 'info') else None
return async_result.task_id, async_result.state, info
except Exception as e: # pylint: disable=broad-except
exception_traceback = f"Celery Task ID: {async_result}\n{traceback.format_exc()}"
return async_result.task_id, ExceptionWithTraceback(e, exception_traceback), None
示例2: retrieve
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def retrieve(self, request, pk=None):
res = AsyncResult(pk)
try:
status = res.status
except AttributeError:
return Response({'message': 'Can\'t get task status'}, status=drf.status.HTTP_400_BAD_REQUEST)
data = {
'status': status
}
if not res.successful():
if res.status == 'PENDING':
data['message'] = 'Task is either waiting, ' \
'does not exist in this context or has been removed after 24h'
else:
data['message'] = res.traceback
else:
data['result'] = res.result
return Response(data, status=drf.status.HTTP_200_OK)
示例3: syncAnsibleResult
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def syncAnsibleResult(self, ret, *a, **kw): # 执行结束,结果保持至db
'''V1 版本'''
# celery_logger.info(self.request.__dict__)
c = AsyncResult(self.request.get('parent_id'))
celery_logger.info(c.result)
tid = kw.get('tid', None)
if tid:
r = redis.Redis(host=REDIS_ADDR, password=REDIS_PD, port=REDIS_PORT, db=ansible_result_redis_db)
a = redis.Redis(host=REDIS_ADDR, password=REDIS_PD, port=REDIS_PORT, db=result_db)
rlist = r.lrange(tid, 0, -1)
at = AnsibleTasks.objects.filter(AnsibleID=tid)[0]
at.AnsibleResult = json.dumps([ json.loads(i.decode()) for i in rlist ])
ct = a.get('celery-task-meta-%s' % at.CeleryID).decode()
at.CeleryResult = ct
at.save()
print("同步结果至db: syncAnsibleResult !!!!!: parent_id: %s" % self.request.get('parent_id'), a, kw)
print('Ansible执行结果:%s' % json.dumps([ json.loads(i.decode()) for i in rlist ]))
else: pass
############ TEST ###########
示例4: prepare_task
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def prepare_task(tuple_type):
data_owner = get_owner()
worker_queue = f"{settings.LEDGER['name']}.worker"
tuples = query_tuples(tuple_type, data_owner)
for subtuple in tuples:
tkey = subtuple['key']
# Verify that tuple task does not already exist
if AsyncResult(tkey).state == 'PENDING':
prepare_tuple.apply_async(
(subtuple, tuple_type),
task_id=tkey,
queue=worker_queue
)
else:
print(f'[Scheduler] Tuple task ({tkey}) already exists')
示例5: consolidate_metadata
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def consolidate_metadata(self, wait=True):
"""
Tries to find an abstract for the paper, if none is available yet,
possibly by fetching it from Zotero via doi-cache.
"""
if self.task is None:
from backend.tasks import consolidate_paper
task = consolidate_paper.delay(pk=self.id)
self.task = task.id
self.save(update_fields=['task'])
else:
task = AsyncResult(self.task)
if wait:
try:
task.get(timeout=10)
finally:
# Clear task if timeout expired.
self.task = None
self.save(update_fields=['task'])
示例6: get
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def get(self, request):
'''
测试接口
'''
try:
json_data = {"message": "ok", "errorCode": 0, "data": {}}
is_open = request.GET.get('is_open')
if is_open == 'mytestkey':
json_data['message'] = '开始了测试'
# 测试cache
# timeout=0 立即过期 timeout=None 永不超时
cache.set("key", "value", timeout=None)
print(cache.get('key'))
# 获取celery的结果
# print(AsyncResult('ec1aab09-003e-46e8-a926-5d9675763709').ready()) # 获取该任务的状态是否完成
# print(AsyncResult('ec1aab09-003e-46e8-a926-5d9675763709').result) # 获取该任务的结果
return Response(json_data)
except Exception as e:
print('发生错误:',e)
return Response({"message": "出现了无法预料的view视图错误:%s" % e, "errorCode": 1, "data": {}})
示例7: task_status
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def task_status(request: Request, task_id: str) -> Response:
if not redis_pool:
raise BackgroundTaskUnavailable()
bg_task = get_object_or_404(request.user.background_tasks, task_id=task_id)
result = AsyncResult(bg_task.task_id)
r = redis.StrictRedis(connection_pool=redis_pool)
details = r.get('tasks:{task_id}:details'.format(task_id=task_id))
if details:
details = details.decode('utf8')
return Response(
{
'id': result.id,
'status': result.state,
'details': json.loads(details or '{}')
}
)
示例8: __add_worker_task
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def __add_worker_task(self, task):
result = AsyncResult(task['id'])
if not task['id'] in self.messages:
try:
timeSubmitted = datetime.fromtimestamp(time.time() - (kombu.five.monotonic() - t['time_start']))
except:
timeSubmitted = str(current_time()) #TODO: dirty hack to make failsafe with UI
self.messages[task['id']] = {
'type': ('train' if 'train' in task['name'] else 'inference'), #TODO
'submitted': timeSubmitted,
'status': celery.states.PENDING,
'meta': {'message':'job at worker'}
}
#TODO: needed?
if result.ready():
result.forget()
示例9: pollNow
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def pollNow(self):
i = self.celery_app.control.inspect()
stats = i.stats()
if stats is not None and len(stats):
active_tasks = i.active()
for key in active_tasks.keys():
taskList = active_tasks[key]
for t in taskList:
taskID = t['id']
if not taskID in self.messages:
# task got lost (e.g. due to server restart); re-add
try:
timeSubmitted = datetime.fromtimestamp(time.time() - (kombu.five.monotonic() - t['time_start']))
except:
timeSubmitted = str(current_time()) #TODO: dirty hack to make failsafe with UI
self.messages[taskID] = {
'type': ('train' if 'train' in t['name'] else 'inference'), #TODO
'submitted': timeSubmitted,
'status': celery.states.PENDING,
'meta': {'message':'job at worker'}
}
job = celery.result.AsyncResult(taskID) #TODO: task.ready()
self.jobs.append(job)
示例10: wait_for_command
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def wait_for_command(request, app_name, task_id, after):
res = AsyncResult(task_id)
if app_name != '_':
app = models.App.objects.get(name=app_name)
task, created = models.TaskLog.objects.get_or_create(task_id=task_id, defaults={'app': app, 'when': datetime.now()})
description = task.description
else:
description = ""
if res.state == state(SUCCESS):
return redirect(reverse(after, kwargs={'app_name': app_name, 'task_id': task_id}))
log = ansi_escape.sub("", get_log(res))
if res.state == state(FAILURE):
log += str(res.traceback)
return render(request, 'command_wait.html', {
'app': app_name,
'task_id': task_id,
'log': log,
'state': res.state,
'running': res.state in [state(PENDING), state(STARTED)],
'description': description
})
示例11: stop
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def stop(self, consumer):
""" This function is called when the worker received a request to terminate.
Upon the termination of the worker, the workflows for all running jobs are
stopped gracefully.
Args:
consumer (Consumer): Reference to the consumer object that handles messages
from the broker.
"""
stopped_workflows = []
for request in [r for r in consumer.controller.state.active_requests]:
job = AsyncResult(request.id)
workflow_id = job.result['workflow_id']
if workflow_id not in stopped_workflows:
client = Client(
SignalConnection(**consumer.app.user_options['config'].signal,
auto_connect=True),
request_key=workflow_id)
client.send(Request(action='stop_workflow'))
stopped_workflows.append(workflow_id)
示例12: __init__
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def __init__(self, task_id, name=None):
result = AsyncResult(task_id)
self.id = task_id
self.name = name
self.finished = result.successful()
self.error = result.failed() or result.state == "CREATOR_ERROR"
self.expired = False
self.pdf = None
self.dl_filename = None
if result.successful():
pdf, dl_filename, clean_task_id = result.result
clean_result = AsyncResult(clean_task_id)
if clean_result.state == 'SUCCESS':
self.expired = True
self.pdf = pdf
self.dl_filename = dl_filename
示例13: results
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def results(self):
"""Fetch result from either the cache or the queue
Raises TimeoutError if results aren't ready by self.timeout
"""
if self.query == "":
return {}
result = cache.get(self.get_cache_key())
if result is None or settings.CELERY_TASK_ALWAYS_EAGER:
search_task = self.search_task()
result = {"task": search_task.id}
cache.set(self.get_cache_key(), result, settings.SEARCH_TIMEOUT)
elif "task" in result:
search_task = AsyncResult(result["task"])
else:
return result
try:
return search_task.get(self.timeout)
except exceptions.TimeoutError:
return result or {}
except Exception:
# some error in the search task, so return an empty dict
return {}
示例14: search_api
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def search_api(request):
try:
key = request.GET["token"]
except KeyError:
raise http.Http404
result = cache.get(key)
if result is not None and "task" in result:
search_task = AsyncResult(result["task"])
try:
search_task.get(TIMEOUT)
except exceptions.TimeoutError:
return http.HttpResponse(status=202) # 202: still waiting for task
except Exception:
return http.HttpResponseBadRequest() # 400: search task errored
return http.HttpResponse(status=201) # 201: search results ready
elif result is not None:
return http.HttpResponse(status=201) # 201: search results ready
else:
return http.HttpResponseBadRequest() # 400: no search is being performed
示例15: import_status
# 需要导入模块: from celery import result [as 别名]
# 或者: from celery.result import AsyncResult [as 别名]
def import_status(request, profile_name):
task_id = request.GET.get('id', request.session.get('import_task_id'))
task = AsyncResult(task_id)
if task:
try:
return JsonResponse({
'status': task.status,
'result': task.result,
})
except:
return JsonResponse({
'status': 'error',
})
else:
raise Http404('Task ID not provided')