Signal django to run a task

后端 未结 2 967
离开以前
离开以前 2020-12-06 23:32

I made a management command that populates one of my models from a csv file.
I need to do this update quite frequently and the csv files have tens of thousands of lines.

2条回答
  •  鱼传尺愫
    2020-12-06 23:46

    Use Celery

    Rougly, it may look like this:

    app = Celery()
    
    @app.task(name='my_task')
    def my_task(self):
        do_stuff()
    
    def my_view(*args, **kwargs):
        result = process_request()
        app.send_task('my_task')
    

    You'll need to create the task, register it with celery (there is some autodiscover magic you can use), then run the task asynchronously from your django app.

    In production, you may want to run celery as a daemon process with celeryd

提交回复
热议问题