Set up a scheduled job?

后端 未结 24 2484
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 01:13

I\'ve been working on a web app using Django, and I\'m curious if there is a way to schedule a job to run periodically.

Basically I just want to run through the dat

相关标签:
24条回答
  • 2020-11-22 02:01

    If you're using a standard POSIX OS, you use cron.

    If you're using Windows, you use at.

    Write a Django management command to

    1. Figure out what platform they're on.

    2. Either execute the appropriate "AT" command for your users, or update the crontab for your users.

    0 讨论(0)
  • 2020-11-22 02:01

    Simple way is to write a custom shell command see Django Documentation and execute it using a cronjob on linux. However i would highly recommend using a message broker like RabbitMQ coupled with celery. Maybe you can have a look at this Tutorial

    0 讨论(0)
  • 2020-11-22 02:02

    Although not part of Django, Airflow is a more recent project (as of 2016) that is useful for task management.

    Airflow is a workflow automation and scheduling system that can be used to author and manage data pipelines. A web-based UI provides the developer with a range of options for managing and viewing these pipelines.

    Airflow is written in Python and is built using Flask.

    Airflow was created by Maxime Beauchemin at Airbnb and open sourced in the spring of 2015. It joined the Apache Software Foundation’s incubation program in the winter of 2016. Here is the Git project page and some addition background information.

    0 讨论(0)
  • 2020-11-22 02:05

    Put the following at the top of your cron.py file:

    #!/usr/bin/python
    import os, sys
    sys.path.append('/path/to/') # the parent directory of the project
    sys.path.append('/path/to/project') # these lines only needed if not on path
    os.environ['DJANGO_SETTINGS_MODULE'] = 'myproj.settings'
    
    # imports and code below
    
    0 讨论(0)
  • 2020-11-22 02:07

    I just thought about this rather simple solution:

    1. Define a view function do_work(req, param) like you would with any other view, with URL mapping, return a HttpResponse and so on.
    2. Set up a cron job with your timing preferences (or using AT or Scheduled Tasks in Windows) which runs curl http://localhost/your/mapped/url?param=value.

    You can add parameters but just adding parameters to the URL.

    Tell me what you guys think.

    [Update] I'm now using runjob command from django-extensions instead of curl.

    My cron looks something like this:

    @hourly python /path/to/project/manage.py runjobs hourly
    

    ... and so on for daily, monthly, etc'. You can also set it up to run a specific job.

    I find it more managable and a cleaner. Doesn't require mapping a URL to a view. Just define your job class and crontab and you're set.

    0 讨论(0)
  • 2020-11-22 02:07

    Django APScheduler for Scheduler Jobs. Advanced Python Scheduler (APScheduler) is a Python library that lets you schedule your Python code to be executed later, either just once or periodically. You can add new jobs or remove old ones on the fly as you please.

    note: I'm the author of this library

    Install APScheduler

    pip install apscheduler
    

    View file function to call

    file name: scheduler_jobs.py

    def FirstCronTest():
        print("")
        print("I am executed..!")
    

    Configuring the scheduler

    make execute.py file and add the below codes

    from apscheduler.schedulers.background import BackgroundScheduler
    scheduler = BackgroundScheduler()
    

    Your written functions Here, the scheduler functions are written in scheduler_jobs

    import scheduler_jobs 
    
    scheduler.add_job(scheduler_jobs.FirstCronTest, 'interval', seconds=10)
    scheduler.start()
    

    Link the File for Execution

    Now, add the below line in the bottom of Url file

    import execute
    
    • You can check the full code by executing [Click here] https://github.com/devchandansh/django-apscheduler
    0 讨论(0)
提交回复
热议问题