我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用celery.task()。
def update(self): self.sqli_obj.scan_status = json.loads(get('{}/scan/{}/status'.format(self.api_url, self.task_id)).text)[ 'status'] try: self.sqli_obj.scan_log = json.loads(get('{}/scan/{}/log'.format(self.api_url, self.task_id)).text)['log'][ -1] self.sqli_obj.scan_data = json.loads(get('{}/scan/{}/data'.format(self.api_url, self.task_id)).text)['data'] except: pass if self.sqli_obj.scan_status != 'terminated': self.update.apply_async((self,), countdown=60) else: get('{}/task/{}/delete'.format(self.api_url, self.task_id)) self.sqli_obj.vulnerable = bool(self.sqli_obj.scan_data) if self.sqli_obj.vulnerable: send_mail('????', "Url:\t{}\n???:\t{}".format(self.sqli_obj.target_url, self.sqli_obj.scan_data[0]['value'][0]['parameter']), self.mail_from, self.mail_to, fail_silently=False) self.sqli_obj.save()
def cron_add_pcbaby_tasks_weekly(): """ ???pcbaby?????????????? """ now = datetime.utcnow() seeds = CrawlerSeeds.objects.filter() queue = settings.TASK_QUEUE_MAPPER.get("seed", {}).get("pcbaby") for seed in seeds: seed.modified_on = now seed.last_crawl_on = now seed.status = "crawling" seed.save() print "Going to put task: %s to queue: %s" % (seed.id, queue) rop.add_task_queue(queue, str(seed.id))
def task_autoretry(*args_task, **kwargs_task): # https://github.com/celery/celery/pull/2112 def real_decorator(func): @task(*args_task, **kwargs_task) @functools.wraps(func) def wrapper(*args, **kwargs): try: func(*args, **kwargs) except kwargs_task.get('autoretry_on', Exception) as exc: for exclude in kwargs_task.get('autoretry_exclude', []): if isinstance(exc, exclude): log.info( 'Wont retry this task because exception ' '"{}" is exclude'.format(str(exc))) return if kwargs_task.get('retry_message', False): log.error(kwargs_task.get('retry_message'), exc_info=True) wrapper.retry(exc=exc) return wrapper return real_decorator
def task_sche_run(): try: print "starting scheduler task" task = Task.objects.filter(status='appointed').filter(sche_time__lte=datetime.datetime.now()) if len(task)>0: for mytask in task: print "mytask_id" print mytask.id hosttag = mytask.dbtag status = 'running' sql = mytask.sqltext mycreatetime = mytask.create_time mytask.status = status mytask.update_time = datetime.datetime.now() mytask.save() log_incep_op(sql, hosttag, mycreatetime) process_runtask.delay(hosttag, sql, mytask) #Process(target=process_runtask, args=).start() except Exception,e: print e
def do_some_async_task(var_1, var_2, *args, **kwargs): """ Do some async task via Celery Usage: do_some_async_task.apply_async( args=[ 'variable 1', 'variable 2', 'arguments 1', 'arguments 2', 'arguments 3' ], kwargs={ 'kwargs_1': 'foo', 'kwargs_2': 'bar' } ) """ return None
def odt_template(fn, ctx, page_size="A4"): inp = zipfile.ZipFile(fn, "r" ) outs = StringIO.StringIO() output = zipfile.ZipFile(outs, "a" ) for zi in inp.filelist: out = inp.read(zi.filename) if zi.filename == 'content.xml': # waut for the only interesting file # un-escape the quotes (in filters etc.) t = Template(out.replace( '"', '"' )) out = t.render(ctx).encode('utf8') if page_size=="US" and zi.filename == 'styles.xml' : t = Template(out.replace( 'style:page-layout-properties fo:page-width="297mm" fo:page-height="210.01mm"', 'style:page-layout-properties fo:page-width="279.4mm" fo:page-height="215.9mm"' )) out = t.render(ctx).encode('utf8') output.writestr(zi.filename, out) output.close() content=outs.getvalue() return content #from celery.task.control import inspect #i = inspect() #i.scheduled() #i.active()
def start(self): self.task_id = json.loads(get('{}/task/new'.format(self.api_url)).text)['taskid'] self.sqli_obj.task_id = self.task_id logging.info(json.dumps(self.scan_options)) res = json.loads(post('{}/option/{}/set'.format(self.api_url, self.task_id), data=json.dumps(self.scan_options), headers=self.headers).text) if res['success']: post('{}/scan/{}/start'.format(self.api_url, self.task_id), data=self.target_url, headers=self.headers) self.update.apply_async((self,), countdown=10) else: self.delete.delay(self)
def delete(self): get('{}/task/{}/delete'.format(self.api_url, self.task_id)) self.sqli_obj.delete()
def storeSatelliteData(request, name): # reviews all the history of the satellite product satelliteObj = SatelliteData.objects.filter(name=name) if not satelliteObj: context = {'message': ('error', 'The satellite data "' + name + '" has not been found in the database.')} else: job = storeSatelliteDataWrapper.delay(name) satelliteObj[0].jobId = job.id satelliteObj[0].save() #======================================================================= # storeSatelliteDataWrapper(name) # satelliteObj = SatelliteData.objects.filter(name=name) # satelliteObj[0].jobId = None #======================================================================= context = {'jobId': satelliteObj[0].jobId, 'message': ('warning', 'Starting data preparation...'), 'state': 'PROGRESS'} # Add celery periodic task intervalSchedules = IntervalSchedule.objects.filter(period='hours', every='2') if intervalSchedules: intervalSchedule = intervalSchedules[0] else: intervalSchedule = IntervalSchedule(period='hours', every='2') intervalSchedule.save() periodicTasks = PeriodicTask.objects.filter(name=name + ' Update') if not periodicTasks: periodicTask = PeriodicTask(name=name + ' Update', task='updateSatelliteData', interval=intervalSchedule, args='["' + name + '"]') periodicTask.save() return JsonResponse(context)
def run_subdomainbrute(target): subdomainbrute_workspace = path.join(TASKS_ROOT, 'tools','subDomainsBrute').replace('\\', '/') cmd = 'subDomainsBrute.py %s -f dict/test_subnames.txt' % target # p=subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ,cwd=subdomainbrute_workspace,) process_output = p.stdout.readlines() return process_output # ??????????300????????5? # @app.task(bind=True, default_retry_delay=300, max_retries=5)
def backend_cleanup(): '''delete all task results from db''' from djcelery.models import TaskState from celery import states query_set = TaskState.objects.exclude(state__in=states.UNREADY_STATES) count = query_set.count() query_set.delete() return 'Deleted: %s task results from db OK' % count
def async_task(): print 'async task'
def add(x, y): request = current_task.request print('Executing task id %r, args: %r kwargs: %r' % ( request.id, request.args, request.kwargs)) return x+y
def execute_dag(self, dag, workflow_id, data=None): """ Celery task that runs a single dag on a worker. This celery task starts, manages and monitors the individual tasks of a dag. Args: self (Task): Reference to itself, the celery task object. dag (Dag): Reference to a Dag object that is being used to start, manage and monitor tasks. workflow_id (string): The unique ID of the workflow run that started this dag. data (MultiTaskData): An optional MultiTaskData object that is being passed to the first tasks in the dag. This allows the transfer of data from dag to dag. """ start_time = datetime.now() logger.info('Running DAG <{}>'.format(dag.name)) # send custom celery event that the dag has been started self.send_event(JobEventName.Started, job_type=JobType.Dag, name=dag.name, time=datetime.utcnow(), workflow_id=workflow_id, duration=None) # store job specific meta information wth the job self.update_state(meta={'name': dag.name, 'type': JobType.Dag, 'workflow_id': workflow_id}) # run the tasks in the DAG signal = DagSignal(Client(SignalConnection(**self.app.user_options['config'].signal, auto_connect=True), request_key=workflow_id), dag.name) dag.run(config=self.app.user_options['config'], workflow_id=workflow_id, signal=signal, data=data) # send custom celery event that the dag has succeeded event_name = JobEventName.Succeeded if not signal.is_stopped else JobEventName.Aborted self.send_event(event_name, job_type=JobType.Dag, name=dag.name, time=datetime.utcnow(), workflow_id=workflow_id, duration=(datetime.now() - start_time).total_seconds()) logger.info('Finished DAG <{}>'.format(dag.name))