我正在使用 Django 1.11。我想在我的应用程序中使用调度程序每天运行一次我的脚本。
这是我的view.py文件
from __future__ import print_function
from django.shortcuts import render
from django.utils import timezone
from django.http import HttpResponse
from datetime import datetime, timedelta
import requests
import schedule
import time
def republic(request):
return HttpResponse("<h1>Success Hindustan</h1>")
def indiatv(request):
return HttpResponse("<h1>Success Hindustan</h1>")
def ndtv(request):
return HttpResponse("<h1>Success NDTV</h1>")
schedule.every().day.at("17:19").do(republic(requests))
schedule.every().day.at("17:19").do(indiatv(requests))
schedule.every().day.at("17:19").do(ndtv(requests))
while 1:
schedule.run_pending()
time.sleep(1)
当我运行服务器时出现以下错误
File "/home/imsaiful/PiroProject/pironews/feed/urls.py", line 2, in <module>
from . import views
File "/home/imsaiful/PiroProject/pironews/feed/views.py", line 230, in <module>
schedule.every().day.at("17:19").do(republic(requests))
File "/home/imsaiful/anaconda3/lib/python3.6/site-packages/schedule/__init__.py", line 385, in do
self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable
但是当我删除调度程序行时,应用程序运行正常。
最佳答案
republic(requests) will return HttpResponse,
所以执行会是
schedule.every().day.at("17:19").do(HttpResponse)
在 do
方法中你应该提到函数,而不是类实例。
您可以使用以下之一
解决方案一:
schedule.every().day.at("17:19").do(lambda: republic(requests))
解决方案 2.
schedule.every().day.at("17:19").do(republic, requests)
解决方案 3.
import functools
schedule.every().day.at("17:19").do(functools.partial(republic, requests))
关于python - Django的TypeError : the first argument must be callable when I import a scheduler in my views. py文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52166054/