【发布时间】:2017-03-16 09:11:45
【问题描述】:
我用 docker 设置了一个 RabbitMQ 服务器,如下所示。然后配置 celery 以将其用作代理。
rabbit:
hostname: rabbit
image: rabbitmq:latest
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=mypass
ports:
- "5673:5672"
worker:
build:
context: .
dockerfile: dockerfile
volumes:
- .:/app
links:
- rabbit
depends_on:
- rabbit
而celery的配置是:
from __future__ import absolute_import
from celery import Celery
app = Celery('test_celery',broker='amqp://admin:mypass@host:5673',backend='rpc://',include=['test_celery.tasks'])
Run_tasks 代码:
from .tasks import longtime_add
import time
if __name__ == '__main__':
url = ['http://example1.com' , 'http://example2.com' , 'http://example3.com' , 'http://example4.com' , 'http://example5.com' , 'http://example6.com' , 'http://example7.com' , 'http://example8.com'] # change them to your ur list.
for i in url:
result = longtime_add.delay(i)
print 'Task result:',result.result
任务代码
from __future__ import absolute_import
from test_celery.celery import app
import time,requests
from pymongo import MongoClient
client = MongoClient('10.1.1.234', 27018) # change the ip and port to your mongo database's
db = client.mongodb_test
collection = db.celery_test
post = db.test
@app.task(bind=True,default_retry_delay=10) # set a retry delay, 10 equal to 10s
def longtime_add(self,i):
try:
r = requests.get(i)
if some conditions happend:
longtime_add.delay(i)
elif some other conditions happened:
post.insert({'status':r.status_code,"creat_time":time.time()})
except Exception as exc:
raise self.retry(exc=exc)
run_taks 代码会生成一个 url 列表并将它们发送到 RabbitMQ,然后任务将消耗它们,并检查某些条件是否发生,如果发生则将结果再次发送到 Rabbtmq,否则将数据存储到数据库。
这里的问题。当任务运行时间较长、24小时甚至更长时间时,RabbitMQ会在“mnesia/rabbit@rabbit/queues”目录下生成大量的idx文件。总大小为 40G。
那么这里的问题,如何停止这些自动生成的大文件或保持小尺寸?
【问题讨论】:
标签: python queue rabbitmq celery