无法使用Django-cron进行函数调用 [英] Unable to make a function call using Django-cron

查看:45
本文介绍了无法使用Django-cron进行函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想每周调用一次方法,为此我执行了一个实现

I want to call a method once in a week for which I made an implementation as mentioned here

https://gutsytechster.wordpress.com/2019/06/24/how-to-setup-a-cron-job-in-django/

我不确定它是如何工作的,但让我解释一下我做了什么.我需要调用下面的文件夹结构中提到的方法.

I am not sure how it works, but let me explain what I did. I need to call a method as mentioned in folder structure below.

proj_application
|
|- myapp
       |
       |-views.py (Method call Inside)
                |- poll_tracked()

在views.py中,

In views.py,

def poll_tracked():
    print('called')

在settings.py中,我已经提到

In settings.py, I have mentioned

INSTALLED_APPS = [
'django_crontab',
]

CRONJOBS = [
    ('* * * * *', 'myapp.views.poll_tracked', '>>' + os.path.join(BASE_DIR, 'data.log'))
]

然后我跑

python3.7 manage.py crontab add
python3.7 manage.py runserver

当我运行crontab -l时,我可以看到

When I run crontab -l, I can see,

* * * * * /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 /Users/domain/dashboard/proj_application/manage.py crontab run dceca84af9ceab8a4d39d08fa148969f >>/Users/domain/dashboard/proj_application/data.log # django-cronjobs for proj_application

将生成一个名为data.log的新日志文件,但是提到的方法poll_tracked()被未调用,并且该日志为空.

A new log file called data.log is generated but the method poll_tracked() mentioned is not called and the logs is empty.

以前有人遇到过这个问题吗?如果是这样,我们将不胜感激.谢谢.

Has anyone faced this problem before? If so, Any help is appreciated. Thanks.

推荐答案

也许只是尝试在最后添加 2>& 1 ,它将错误输出重定向到标准输出,并且可能会解释为什么您的日志文件为空.另外,您在>>

Maybe just try adding 2>&1 at the end, it redirects the error output to the standard output, and might explain why your log file is empty. Also you are missing a space after >>

CRONJOBS = [
    ('* * * * *', 'myapp.views.poll_tracked', '>> ' + os.path.join(BASE_DIR, 'data.log') + ' 2>&1')
]

这篇关于无法使用Django-cron进行函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆