def taskstatus(task_id): task = long_task.AsyncResult(task_id) if task.state == 'PENDING': # esta es cuando el 'task' no ha comenzado por eso muestra pendiente response = { # respondera con un json que contenga state, current, total y status 'state': task.state, # state sera llevado por task.state 'current': 0, # current sera 0 'total': 1, # total sera 1 'status': 'Pending...' # estatus mostrara la palabra pendiente } elif task.state != 'FAILURE': # si diferente de failure response = { # aqui obtiene los datos utilizando task.info.get para cada uno de las variables 'state': task.state, # state sera task.state 'current': task.info.get( 'current', 0 ), # current sera task.info.get (current) no se por que el cero 'total': task.info.get('total', 1), #lo mismo hace para total y status 'status': task.info.get('status', '') } if 'result' in task.info: response['result'] = task.info[ 'result'] # cuando encuentre un resultdo en task.info respodera con la respuesta else: # something went wrong in the background job response = { # esto es si task.state = failure 'state': task.state, 'current': 1, 'total': 1, 'status': str( task.info ), # this is the exception raised ! aqui dira exatamente que paso al usuario } return jsonify( response) # despues de esto regresara una respuesta utilizando jsonify
def taskstatus(task_id): task = long_task.AsyncResult(task_id) # Background task at work if task.state == 'working': print('RECEIVED STATE : working') response = {'state': task.state, 'info': task.info} # Background task is done elif task.state == 'SUCCESS': print('RECEIVED STATE : SUCCESS') result = task.info['info'] # The api call returned something if result['query_status'] is True: print('No data found in query result') # No results found after api call if result['data'] is None: response = {'state': 'empty', 'dir': result['dir']} # Data available after api call else: print('data available in query result') print('SENDING TASK') print(result['dir']) print(os.listdir('{}/public/'.format(app.root_path))) if not os.path.isdir('{}/public/{}/'.format( app.root_path, result['dir'])): os.mkdir('{}/public/{}/'.format(app.root_path, result['dir'])) items_to_csv( result['data'], '{}/public/{}/output.csv'.format( app.root_path, result['dir'])) response = { 'state': 'loaded', 'data': result['data'], 'dir': result['dir'] } # There was an error during api call else: response = { 'state': 'error', 'dir': result['dir'], 'error': result['error'] } # Something went wrong the the background task else: print('unseen query state :', task.state) response = { 'state': 'error', 'dir': '', 'error': 'Problème lors de la gestion de la requête' } return jsonify(response)
def taskstatus(task_id): task = long_task.AsyncResult(task_id) if task.state == 'PENDING': # job did not start yet response = { 'state': task.state, 'current': 0, 'total': 1, 'status': 'Pending...' } elif task.state != 'FAILURE': response = { 'state': task.state, 'current': task.info.get('current', 0), 'total': task.info.get('total', 1), 'status': task.info.get('status', '') } if 'result' in task.info: response['result'] = task.info['result'] else: # something went wrong in the background job response = { 'state': task.state, 'current': 1, 'total': 1, 'status': str(task.info), # this is the exception raised } return jsonify(response)
from tasks import add, fail, long_task tsk = long_task.apply_async(args=[23]) resp = long_task.AsyncResult(tsk.id) result = add.delay(4, 4) result.apply_async result.ready() result.get(timeout=1) result.get(propagate=False) result.traceback result = fail.delay(4) result.ready() result.get(timeout=1) result.get(propagate=False) result.traceback