为什么“heroku运行python manage.py send_queued_mail”在命令行中工作,但在Heroku的clock.py文件中却没有?

时间:2014-01-24 01:36:14

标签: django heroku apscheduler

我的clock.py下面。我可以整天使用heroku run python manage.py send_queued_mail,但在clock.py文件中包含run python manage.py send_queued_mail会导致语法错误。

clock.py:

from apscheduler.scheduler import Scheduler

sched = Scheduler()

@sched.interval_schedule(minutes=1)
def timed_job():
    python manage.py send_queued_mail

sched.start()

while True:
    pass

Heroku错误消息(来自日志):

2014-01-24T01:31:47.256648+00:00 app[clock.1]:   File "clock.py", line 7
2014-01-24T01:31:47.256648+00:00 app[clock.1]:     run python manage.py send_queued_mail
2014-01-24T01:31:47.256648+00:00 app[clock.1]:              ^
2014-01-24T01:31:47.256648+00:00 app[clock.1]: SyntaxError: invalid syntax

编辑: management.call_command('send_queued_mail')在heroku上的python shell中运行,但是当我从clock.py文件运行它时失败并出现“command not found”错误。我可以清楚地看到send_queued_email.py文件应该在哪里。如果我运行python manage.py help,我可以看到1send_queued_email`作为可用命令。以某种方式从clock.py运行命令导致heroku不喜欢命令。

2 个答案:

答案 0 :(得分:2)

由于

python manage.py send_queued_mail

不是有效的Python代码。

您可以使用django.core.management.call_command以编程方式调用管理命令:

from django.core.management import call_command
call_command('send_queued_mail')

答案 1 :(得分:1)

我终于弄明白了。从python文件调用我的命令时,我需要将DJANGO_SETTINGS_MODULE显式设置为mysite.settings。以下是其他任何人决定要从Heroku上的Django应用程序安排异步电子邮件发送的最终文件,其频率高于Heroku Scheduler附加组件支持的每5分钟一次。

# clock.py

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'anonqaproject.settings'

from apscheduler.scheduler import Scheduler
from post_office import utils

sched = Scheduler()

@sched.interval_schedule(minutes=1)
def timed_job():
    utils.send_queued_mail()

sched.start()

while True:
    pass
相关问题