Django-Celeryの非同期タスクとタイミングタスク


一、Celery非同期任務
# -----          
# 1、  Django    
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'zufang.settings')

# 2、  Celery  ,     、    (    )      
app = celery.Celery(
    'zufang', # 
    broker='redis://120.77.222.217:6379/1', #     
    backend='redis://120.77.222.217:6379/2' #     
    # broker='amqp://luohao:[email protected]:5672/zufangwang_vhost',
    # backend='django-db' #    mysql              
	{
     
		1、pip install django-celery-results
		2、INSTALLED_APPS = ['django_celery_results']
		3、python manage.py migrate django_celery_results
		#      ,      
		{
     
			1、  django_celery_results   django_migrations
			2、     : sql_safe_updates   OFF
					show variables like '%safe%'
					set session sql_safe_updates = off;
					set global sql_safe_updates = off;
			3、      :python manage.py migrate django_celery_results
		}
		4、             (    /    )
			app.autodiscover_tasks(('  ', ))
	}
)
# ------       
QINIU_ACCESS_KEY = 'KarvlHfUdoG1mZNSfDVS5Vh3nae2jUZumTBHK-PR'
QINIU_SECRET_KEY = 'SFPFkAn5NENhdCMqMe9wd_lxGHAeFR5caXxPTtt7'
AUTH = qiniu.Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)

#       
@app.task
def upload_file_to_qiniu(file_path, filename):
	token = AUTH.upload_token(QINIU_BUCKET_NAME, filename)
	return qiniu.put_file(token, filename, file_path)

#       ,      
{
     
	1、upload_file_to_qiniu.delay(file_path,filename)
	2、task = upload_file_to_qiniu.s(countdown=     ,expires=      )
		task.delay(file_path,filename)
	3、upload_file_to_qiniu.applay_async((file_path,filename),
				queue='queue1',countdown=10, #       ,    
				retry_policy={
     'max_reties':3}, #       
				expires = 60,compression='zlib') #     ,    
				)
}
#      
#      

二、Celeryタイミングタスク
# ---------    
from celery.schedules import crontab
app.conf.update(
			timezone=settings.TIME_ZONE,
			enable_utc=True,
			beat_schedule={
     
				'task':{
       #    
					'task':'common.tasks.display_info', #       ----    
					'schedule':crontab('*','*','*','*','*'), #----           
					'args':('hello,world',) #  display_info  
					},
				'task2':{
     }, #    
				})
# ------------     
@app.tast
def display_info(conntent):
	print(content)

#          

Celeryメッセージキューの監視:
pip install flower
celery flower --broker = amqp://luohao:  @  :  /   (      )

Celery原語:
1-group -                 

		from celery import group
		task_group = group(task1(),task2(),...)
		result = task_group() --- list
		
2-chain-                           
		from celery import chain
		task = chain(task1.s()|task2.s()|...)
		result = task()