celery 基础教程(三):配置
前言
如果你使用默认的加载器,你必须创建 celeryconfig.py 模块并且保证它在python路径中。
一、加载配置方法
1.基础方法
可以设置一些选项来改变 Celery 的工作方式。这些选项可以直接在 app 实例上进行设置,或者也可以使用一个指定的配置模块
配置使用 app.conf
变量保存:
>>> app.conf.timezone 'Europe/London'
你可以直接设置配置值:
>>> app.conf.enable_utc = True
或者使用 update
方法同时更新多个配置项。
>>> app.conf.update( ... enable_utc=True, ... timezone='Europe/London', ...)
实际的配置对象由多个字典决定,配置项按以下顺序查询:
1. 运行时的配置修改
2. 配置模块(如果声明)
3. 默认配置(celery.app.defaults)
你还可以使用 app.add_defaults()
方法添加新的默认配置源。
2.config_from_object
app.config_from_object() 方法从一个配置对象加载配置。
它可以是一个配置模块,或者任意包含配置属性的对象。
注意任何前面设置的配置在调用 config_from_object
方法后都将被重置。如果你想设置附加的配置应该在调用这个方法之后。
示例1: 使用模块名app.config_from_object()
方法的参数可以是一个 python 模块的全限定名称,或者一个 python 属性名,例如:”celeryconfig”,”myproj.config.celery”,或者 “myproj.config:CeleryConfig”:
from celery import Celery app = Celery() app.config_from_object('celeryconfig')
celeryconfig 模块内容如下形式:
celeryconfig.py:
enable_utc = True timezone = 'Europe/London'
只要 import celeryconfig
能正常运行,应用实例就能加载它。
示例2:传递一个模块对象
你还可以传递一个已经加载的模块对象,但是不作为常规建议。
建议使用模块名的方式加载,因为这种情况下当prefork池使用时,配置模块不必序列化。如果遇到配置问题或者序列化错误,可以尝试使用模块名的方式加载配置。
import celeryconfig from celery import Celery app = Celery() app.config_from_object(celeryconfig)
示例3:使用配置类/对象
from celery import Celery app = Celery() class Config: enable_utc = True timezone = 'Europe/London' app.config_from_object(Config) # or using the fully qualified name of the object: # app.config_from_object('module:Config')
二、配置文件示例
以下是配置示例,你可以从这个开始。它包括运行一个基本Celery应用的所有基础设置。
## Broker settings. broker_url = 'amqp://guest:guest@localhost:5672//' # List of modules to import when the Celery worker starts. imports = ('myapp.tasks',) ## Using the database to store task state and results. result_backend = 'db+sqlite:///results.db' task_annotations = {'tasks.add': {'rate_limit': '10/s'}}
三、新的小写设置
与以前版本的不同,除了设置项名称变为小写字母外,还有一个前缀的重命名,例如 celerybeat_ 变为 beat_,celeryd_ 变为 worker,以及很多顶级 celery_ 设置重命名成了 task_ 前缀。
Celery 仍然能读取老的配置文件,所以并不仓促迁移到新的设置格式。
Setting name | Replace with |
---|---|
CELERY_ACCEPT_CONTENT | accept_content |
CELERY_ENABLE_UTC | enable_utc |
CELERY_IMPORTS | imports |
CELERY_INCLUDE | include |
CELERY_TIMEZONE | timezone |
CELERYBEAT_MAX_LOOP_INTERVAL | beat_max_loop_interval |
CELERYBEAT_SCHEDULE | beat_schedule |
CELERYBEAT_SCHEDULER | beat_scheduler |
CELERYBEAT_SCHEDULE_FILENAME | beat_schedule_filename |
CELERYBEAT_SYNC_EVERY | beat_sync_every |
BROKER_URL | broker_url |
BROKER_TRANSPORT | broker_transport |
BROKER_TRANSPORT_OPTIONS | broker_transport_options |
BROKER_CONNECTION_TIMEOUT | broker_connection_timeout |
BROKER_CONNECTION_RETRY | broker_connection_retry |
BROKER_CONNECTION_MAX_RETRIES | broker_connection_max_retries |
BROKER_FAILOVER_STRATEGY | broker_failover_strategy |
BROKER_HEARTBEAT | broker_heartbeat |
BROKER_LOGIN_METHOD | broker_login_method |
BROKER_POOL_LIMIT | broker_pool_limit |
BROKER_USE_SSL | broker_use_ssl |
CELERY_CACHE_BACKEND | cache_backend |
CELERY_CACHE_BACKEND_OPTIONS | cache_backend_options |
CASSANDRA_COLUMN_FAMILY | cassandra_table |
CASSANDRA_ENTRY_TTL | cassandra_entry_ttl |
CASSANDRA_KEYSPACE | cassandra_keyspace |
CASSANDRA_PORT | cassandra_port |
CASSANDRA_READ_CONSISTENCY | cassandra_read_consistency |
CASSANDRA_SERVERS | cassandra_servers |
CASSANDRA_WRITE_CONSISTENCY | cassandra_write_consistency |
CELERY_COUCHBASE_BACKEND_SETTINGS | couchbase_backend_settings |
CELERY_MONGODB_BACKEND_SETTINGS | mongodb_backend_settings |
CELERY_EVENT_QUEUE_EXPIRES | event_queue_expires |
CELERY_EVENT_QUEUE_TTL | event_queue_ttl |
CELERY_EVENT_QUEUE_PREFIX | event_queue_prefix |
CELERY_EVENT_SERIALIZER | event_serializer |
CELERY_REDIS_DB | redis_db |
CELERY_REDIS_HOST | redis_host |
CELERY_REDIS_MAX_CONNECTIONS | redis_max_connections |
CELERY_REDIS_PASSWORD | redis_password |
CELERY_REDIS_PORT | redis_port |
CELERY_RESULT_BACKEND | result_backend |
CELERY_MAX_CACHED_RESULTS | result_cache_max |
CELERY_MESSAGE_COMPRESSION | result_compression |
CELERY_RESULT_EXCHANGE | result_exchange |
CELERY_RESULT_EXCHANGE_TYPE | result_exchange_type |
CELERY_TASK_RESULT_EXPIRES | result_expires |
CELERY_RESULT_PERSISTENT | result_persistent |
CELERY_RESULT_SERIALIZER | result_serializer |
CELERY_RESULT_DBURI | Use result_backend instead. |
CELERY_RESULT_ENGINE_OPTIONS | database_engine_options |
[…]_DB_SHORT_LIVED_SESSIONS | database_short_lived_sessions |
CELERY_RESULT_DB_TABLE_NAMES | database_db_names |
CELERY_SECURITY_CERTIFICATE | security_certificate |
CELERY_SECURITY_CERT_STORE | security_cert_store |
CELERY_SECURITY_KEY | security_key |
CELERY_TASK_ACKS_LATE | task_acks_late |
CELERY_TASK_ALWAYS_EAGER | task_always_eager |
CELERY_TASK_ANNOTATIONS | task_annotations |
CELERY_TASK_COMPRESSION | task_compression |
CELERY_TASK_CREATE_MISSING_QUEUES | task_create_missing_queues |
CELERY_TASK_DEFAULT_DELIVERY_MODE | task_default_delivery_mode |
CELERY_TASK_DEFAULT_EXCHANGE | task_default_exchange |
CELERY_TASK_DEFAULT_EXCHANGE_TYPE | task_default_exchange_type |
CELERY_TASK_DEFAULT_QUEUE | task_default_queue |
CELERY_TASK_DEFAULT_RATE_LIMIT | task_default_rate_limit |
CELERY_TASK_DEFAULT_ROUTING_KEY | task_default_routing_key |
CELERY_TASK_EAGER_PROPAGATES | task_eager_propagates |
CELERY_TASK_IGNORE_RESULT | task_ignore_result |
CELERY_TASK_PUBLISH_RETRY | task_publish_retry |
CELERY_TASK_PUBLISH_RETRY_POLICY | task_publish_retry_policy |
CELERY_TASK_QUEUES | task_queues |
CELERY_TASK_ROUTES | task_routes |
CELERY_TASK_SEND_SENT_EVENT | task_send_sent_event |
CELERY_TASK_SERIALIZER | task_serializer |
CELERYD_TASK_SOFT_TIME_LIMIT | task_soft_time_limit |
CELERYD_TASK_TIME_LIMIT | task_time_limit |
CELERY_TRACK_STARTED | task_track_started |
CELERYD_AGENT | worker_agent |
CELERYD_AUTOSCALER | worker_autoscaler |
CELERYD_CONCURRENCY | worker_concurrency |
CELERYD_CONSUMER | worker_consumer |
CELERY_WORKER_DIRECT | worker_direct |
CELERY_DISABLE_RATE_LIMITS | worker_disable_rate_limits |
CELERY_ENABLE_REMOTE_CONTROL | worker_enable_remote_control |
CELERYD_HIJACK_ROOT_LOGGER | worker_hijack_root_logger |
CELERYD_LOG_COLOR | worker_log_color |
CELERYD_LOG_FORMAT | worker_log_format |
CELERYD_WORKER_LOST_WAIT | worker_lost_wait |
CELERYD_MAX_TASKS_PER_CHILD | worker_max_tasks_per_child |
CELERYD_POOL | worker_pool |
CELERYD_POOL_PUTLOCKS | worker_pool_putlocks |
CELERYD_POOL_RESTARTS | worker_pool_restarts |
CELERYD_PREFETCH_MULTIPLIER | worker_prefetch_multiplier |
CELERYD_REDIRECT_STDOUTS | worker_redirect_stdouts |
CELERYD_REDIRECT_STDOUTS_LEVEL | worker_redirect_stdouts_level |
CELERYD_SEND_EVENTS | worker_send_task_events |
CELERYD_STATE_DB | worker_state_db |
CELERYD_TASK_LOG_FORMAT | worker_task_log_format |
CELERYD_TIMER | worker_timer |
CELERYD_TIMER_PRECISION | worker_timer_precision |
本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/12706135.html