django/celery: Best practices to run tasks on 150k Django objects?

前端 未结 3 1377
南方客
南方客 2021-02-03 12:43

I have to run tasks on approximately 150k Django objects. What is the best way to do this? I am using the Django ORM as the Broker. The database backend is MySQL and chokes a

3条回答
  •  伪装坚强ぢ
    2021-02-03 13:03

    I use beanstalkd ( http://kr.github.com/beanstalkd/ ) as the engine. Adding a worker and a task is pretty straightforward for Django if you use django-beanstalkd : https://github.com/jonasvp/django-beanstalkd/

    It’s very reliable for my usage.

    Example of worker :

    import os
    import time
    
    from django_beanstalkd import beanstalk_job
    
    
    @beanstalk_job
    def background_counting(arg):
        """
        Do some incredibly useful counting to the value of arg
        """
        value = int(arg)
        pid = os.getpid()
        print "[%s] Counting from 1 to %d." % (pid, value)
        for i in range(1, value+1):
            print '[%s] %d' % (pid, i)
            time.sleep(1)
    

    To launch a job/worker/task :

    from django_beanstalkd import BeanstalkClient
    client = BeanstalkClient()
    
    client.call('beanstalk_example.background_counting', '5')
    

    (source extracted from example app of django-beanstalkd)

    Enjoy !

提交回复
热议问题