本文整理汇总了Python中celery.Celery方法的典型用法代码示例。如果您正苦于以下问题:Python celery.Celery方法的具体用法?Python celery.Celery怎么用?Python celery.Celery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类celery
的用法示例。
在下文中一共展示了celery.Celery方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make_celery
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def make_celery(app):
# create context tasks in celery
celery = Celery(app.import_name, broker=app.config['BROKER_URL'])
celery.config_from_object(celeryconfig)
# celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
示例2: plugin
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def plugin(srv, item):
srv.logging.debug("*** MODULE=%s: service=%s, target=%s", __file__, item.service, item.target)
config = item.config
app = celery.Celery(
config['app_name'],
broker=config['broker_url']
)
for target in item.addrs:
message = item.message
try:
if target['message_format'] == 'json':
message = json.loads(message)
app.send_task(target['task'], [message])
except Exception as e:
srv.logging.warning("Error: %s" % e)
return False
return True
示例3: index
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def index():
if request.method == 'GET':
return render_template('index.html', email=session.get('email', ''))
email = request.form['email']
session['email'] = email
# send the email
email_data = {
'subject': 'Hello from Flask',
'to': email,
'body': 'This is a test email sent from a background Celery task.'
}
if request.form['submit'] == 'Send':
# send right away
send_async_email.delay(email_data)
flash('Sending email to {0}'.format(email))
else:
# send in one minute
send_async_email.apply_async(args=[email_data], countdown=60)
flash('An email will be sent to {0} in one minute'.format(email))
return redirect(url_for('index'))
示例4: make_celery
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def make_celery(app):
celery = Celery(
app.import_name,
broker=app.config['CELERY_BROKER_URL'],
backend=app.config['CELERY_RESULT_BACKEND']
)
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
示例5: test_no_stackoverflows
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def test_no_stackoverflows(celery):
"""We used to have a bug in the Celery integration where its monkeypatching
was repeated for every task invocation, leading to stackoverflows.
See https://github.com/getsentry/sentry-python/issues/265
"""
results = []
@celery.task(name="dummy_task")
def dummy_task():
with configure_scope() as scope:
scope.set_tag("foo", "bar")
results.append(42)
for _ in range(10000):
dummy_task.delay()
assert results == [42] * 10000
with configure_scope() as scope:
assert not scope._tags
示例6: make_celery
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL']
)
celery.conf.update(app.config)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
class ContextQueueOnce(QueueOnce):
def __call__(self, *args, **kwargs):
with app.app_context():
return super(ContextQueueOnce, self).__call__(*args, **kwargs)
celery.Task = ContextTask
celery.QueueOnce = ContextQueueOnce
return celery
示例7: make_celery
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config['CELERY_RESULT_BACKEND'],
broker=app.config['CELERY_BROKER_URL']
)
celery.conf.update(app.config)
celery.config_from_object(celeryconfig)
class ContextTask(celery.Task):
def __call__(self, *args, **kwargs):
with app.app_context():
return self.run(*args, **kwargs)
celery.Task = ContextTask
return celery
示例8: _process_tasks
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def _process_tasks(self, task_tuples_to_send: List[TaskInstanceInCelery]) -> None:
first_task = next(t[4] for t in task_tuples_to_send)
# Celery state queries will stuck if we do not use one same backend
# for all tasks.
cached_celery_backend = first_task.backend
key_and_async_results = self._send_tasks_to_celery(task_tuples_to_send)
self.log.debug('Sent all tasks.')
for key, _, result in key_and_async_results:
if isinstance(result, ExceptionWithTraceback):
self.log.error( # pylint: disable=logging-not-lazy
CELERY_SEND_ERR_MSG_HEADER + ":%s\n%s\n", result.exception, result.traceback
)
elif result is not None:
# Only pops when enqueued successfully, otherwise keep it
# and expect scheduler loop to deal with it.
self.queued_tasks.pop(key)
result.backend = cached_celery_backend
self.running.add(key)
self.tasks[key] = result
self.last_state[key] = celery_states.PENDING
示例9: update_task_state
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def update_task_state(self, key: TaskInstanceKeyType, state: str, info: Any) -> None:
"""Updates state of a single task."""
# noinspection PyBroadException
try:
if self.last_state[key] != state:
if state == celery_states.SUCCESS:
self.success(key, info)
del self.tasks[key]
del self.last_state[key]
elif state == celery_states.FAILURE:
self.fail(key, info)
del self.tasks[key]
del self.last_state[key]
elif state == celery_states.REVOKED:
self.fail(key, info)
del self.tasks[key]
del self.last_state[key]
else:
self.log.info("Unexpected state: %s", state)
self.last_state[key] = state
except Exception: # pylint: disable=broad-except
self.log.exception("Error syncing the Celery executor, ignoring it.")
示例10: fetch_celery_task_state
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [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
示例11: celery
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def celery(self):
app = self.flask_app
celery = Celery(app.import_name, broker=app.config[
'CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
示例12: __init__
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def __init__(
self,
broker_url,
listen_address,
max_tasks=10000,
namespace="celery",
transport_options=None,
enable_events=False,
):
self._listen_address = listen_address
self._max_tasks = max_tasks
self._namespace = namespace
self._enable_events = enable_events
self._app = celery.Celery(broker=broker_url)
self._app.conf.broker_transport_options = transport_options or {}
示例13: make_celery
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def make_celery(self, backend=None, broker=None):
if backend:
self.config['CELERY_RESULT_BACKEND'] = backend
if broker:
self.config['CELERY_BROKER_URL'] = broker
celery = Celery(
self.import_name,
backend=self.config['CELERY_RESULT_BACKEND'],
broker=self.config['CELERY_BROKER_URL']
)
celery.conf.update(self.config)
class ContextTask(celery.Task):
def __call__(s, *args, **kwargs):
with self.app_context():
return s.run(*args, **kwargs)
celery.Task = ContextTask
return celery
示例14: make_celery
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def make_celery(app):
celery = Celery(
app.import_name,
backend=app.config.get("CELERY_RESULT_BACKEND"),
broker=app.config.get("CELERY_BROKER_URL"),
)
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
示例15: report_failed_task
# 需要导入模块: import celery [as 别名]
# 或者: from celery import Celery [as 别名]
def report_failed_task(**kwargs):
"""
Report a generic failure metric as tasks to our metrics broker every time a task fails.
This metric can be used for alerting.
https://docs.celeryproject.org/en/latest/userguide/signals.html#task-failure
"""
with flask_app.app_context():
log_data = {
"function": f"{__name__}.{sys._getframe().f_code.co_name}",
"Message": "Celery Task Failure",
}
# Add traceback if exception info is in the kwargs
einfo = kwargs.get("einfo")
if einfo:
log_data["traceback"] = einfo.traceback
error_tags = get_celery_request_tags(**kwargs)
log_data.update(error_tags)
current_app.logger.error(log_data)
metrics.send("celery.failed_task", "TIMER", 1, metric_tags=error_tags)