Python 使用apscheduler for Django项目在Procfile(Heroku)中定义Cron作业的问题

Python 使用apscheduler for Django项目在Procfile(Heroku)中定义Cron作业的问题,python,django,heroku,cron,apscheduler,Python,Django,Heroku,Cron,Apscheduler,我在安排cron作业时遇到了一个问题,这需要抓取一个网站并将其作为模型(电影)的一部分存储在数据库中 问题是模型似乎在执行Procfile之前就被加载了 我应该如何创建一个cron作业,它在后台内部运行,并将刮取的信息存储到数据库中?这是我的密码: 程序文件: web: python manage.py runserver 0.0.0.0:$PORT scheduler: python cinemas/scheduler.py scheduler.py: # More code

我在安排cron作业时遇到了一个问题,这需要抓取一个网站并将其作为模型(电影)的一部分存储在数据库中

问题是模型似乎在执行Procfile之前就被加载了
我应该如何创建一个cron作业,它在后台内部运行,并将刮取的信息存储到数据库中?这是我的密码:

程序文件:

    web: python manage.py runserver 0.0.0.0:$PORT
    scheduler: python cinemas/scheduler.py
scheduler.py:

# More code above
from cinemas.models import Movie
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()

@sched.scheduled_job('cron', day_of_week='mon-fri', hour=0, minutes=26)    
def get_movies_playing_now():
  global url_movies_playing_now
  Movie.objects.all().delete()
  while(url_movies_playing_now):
    title = []
    description = []
    #Create BeatifulSoup Object with url link
    s = requests.get(url_movies_playing_now, headers=headers)
    soup = bs4.BeautifulSoup(s.text, "html.parser")
    movies = soup.find_all('ul', class_='w462')[0]

    #Find Movie's title
    for movie_title in movies.find_all('h3'):
        title.append(movie_title.text)
    #Find Movie's description
    for movie_description in soup.find_all('ul',
                                           class_='w462')[0].find_all('p'):
        description.append(movie_description.text.replace(" [More]","."))

    for t, d in zip(title, description):
        m = Movie(movie_title=t, movie_description=d)
        m.save()

    #Go to the next page to find more movies
    paging = soup.find( class_='pagenating').find_all('a', class_=lambda x:
                                                      x != "inactive")
    href = ""
    for p in paging:
        if "next" in p.text.lower():
            href = p['href']
    url_movies_playing_now = href

sched.start()
# More code below
cinemas/models.py:

from django.db import models

#Create your models here.

class Movie(models.Model):
    movie_title = models.CharField(max_length=200)
    movie_description = models.CharField(max_length=20200)
这是我在运行作业时遇到的错误

2016-11-17T17:57:06.074914+00:00应用程序[调度程序1]:回溯(most 最近一次通话(最后一次):2016-11-17T17:57:06.074931+00:00应用程序[调度程序1]: 文件“cinemas/scheduler.py”,第2行,在 2016-11-17T17:57:06.075058+00:00应用程序[调度程序1]:导入cineplex 2016-11-17T17:57:06.075060+00:00应用程序[scheduler.1]:文件 “/app/cinemas/cineplex.py”,第1行,在 2016-11-17T17:57:06.075173+00:00应用程序[调度程序1]:来自 cinemas.models导入电影2016-11-17T17:57:06.075196+00:00 app[scheduler.1]:文件“/app/cinemas/models.py”,第5行,在 2016-11-17T17:57:06.075295+00:00应用程序[调度程序1]:类 电影(模特。模特):2016-11-17T17:57:06.075297+00:00 app[scheduler.1]:文件 “/app/.heroku/python/lib/python3.5/site packages/django/db/models/base.py”, 第105行,在2016-11-17T17:57:06.075414+00:00中 应用程序[scheduler.1]:应用程序配置= apps.get_包含_app_配置(模块) 2016-11-17T17:57:06.075440+00:00应用程序[scheduler.1]:文件 “/app/.heroku/python/lib/python3.5/site packages/django/apps/registry.py”, 第237行,在包含应用配置的get_中 2016-11-17T17:57:06.075585+00:00应用程序[调度程序1]:
自我检查应用程序就绪()2016-11-17T17:57:06.075586+00:00 app[scheduler.1]:文件 “/app/.heroku/python/lib/python3.5/site packages/django/apps/registry.py”, 第124行,检查应用程序就绪2016-11-17T17:57:06.075703+00:00 应用程序[scheduler.1]:引发AppRegistryNotReady(“未加载应用程序 但是“)2016-11-17T17:57:06.075726+00:00应用程序[scheduler.1]: django.core.exceptions.AppRegistryNotReady:尚未加载应用程序

如果不包括模型对象,Cron作业就可以正常工作。如何每天使用模型对象运行此作业而不失败


谢谢

这是因为您不能只导入Django软件包、模型等。
为了正常工作,Django内部需要初始化,这是从
manage.py
触发的

我总是编写长时间运行的非web命令,而不是自己尝试重新创建这些命令

例如,如果你的应用程序是电影院,你会:

  • 创建
    /cinemas/management/commands/scheduler.py
  • 在该文件中,创建一个子类
    django.core.management.base.BaseCommand
    (该子类必须称为
    Command
  • 在该类中,重写
    handle()
    。在您的情况下,您可以在这里调用
    sched.start()
  • 然后,您的
    Procfile
    将具有
    scheduler:python manage.py scheduler

希望对您有所帮助。

您可以通过在sceduler.py顶部添加以下行来解决此问题

import django
django.setup()
在django文档中

如果您正在使用Django“standalone”的组件——例如,编写一个Python脚本来加载一些Django模板并呈现它们,或者使用ORM获取一些数据——除了配置设置之外,您还需要另外一个步骤

在设置DJANGO_SETTINGS_模块或调用configure()之后,需要调用DJANGO.setup()来加载设置并填充DJANGO的应用程序注册表。例如:

import django
from django.conf import settings
from myapp import myapp_defaults

settings.configure(default_settings=myapp_defaults, DEBUG=True)
django.setup()

# Now this script or any imported module can use any part of Django it needs.
from myapp import models
我将DJANGO_SETTINGS_模块设置为配置变量,因此没有将其添加到我的计划程序中