Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:在django的views.py文件中导入调度程序时,第一个参数必须可调用?_Python_Django_Python 3.x_Django Views - Fatal编程技术网

Python TypeError:在django的views.py文件中导入调度程序时,第一个参数必须可调用?

Python TypeError:在django的views.py文件中导入调度程序时,第一个参数必须可调用?,python,django,python-3.x,django-views,Python,Django,Python 3.x,Django Views,我正在使用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 r

我正在使用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)
from\uuuuu future\uuuuu导入打印功能
从django.shortcuts导入渲染
从django.utils导入时区
从django.http导入HttpResponse
从datetime导入datetime,timedelta
导入请求
进口时间表
导入时间
def共和国(请求):
返回HttpResponse(“成功印度斯坦”)
def indiatv(请求):
返回HttpResponse(“成功印度斯坦”)
def ndtv(请求):
返回HttpResponse(“成功NDTV”)
日程安排:每()天(“17:19”).do(共和国(请求))
日程安排:每()天(“17:19”).do(印度电视台(请求))
日程安排:每()天(“17:19”).do(ndtv(请求))
而1:
schedule.run_pending()
时间。睡眠(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
文件“/home/imsaiful/PiroProject/pironews/feed/url.py”,第2行,在
从…起导入视图
文件“/home/imsaiful/PiroProject/pironews/feed/views.py”,第230行,在
日程安排:每()天(“17:19”).do(共和国(请求))
文件“/home/imsaiful/anaconda3/lib/python3.6/site packages/schedule/_init__.py”,do中第385行
self.job_func=functools.partial(job_func,*args,**kwargs)
TypeError:第一个参数必须是可调用的
但是当我删除调度程序行时,应用程序就会正常运行

根据,应在函数后传递参数:

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)
do(作业函数,*args,**kwargs)

指定每次运行作业时应调用的作业函数。 作业运行时,任何附加参数都会传递给作业_func

共和国(请求)将返回HttpResponse

因此,执行将是

schedule.every().day.at("17:19").do(HttpResponse)
do
方法中,您应该提到函数,而不是类实例。 您可以使用以下选项之一

解决方案1:

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))
正在修复计划调用 以书面形式:

schedule.every().day.at("17:19").do(republic(requests))
schedule.every().day.at("17:19").do(republic(requests))
schedule.every().day.at("17:19").do(republic(requests))
在特定线程中运行调度程序 在Django中不能以这种方式运行调度程序,因为这意味着您在加载文件时将继续运行该命令。因此,加载文件永远不会终止,因此服务器永远不会启动

但是,您可以通过以下方式异步运行计划程序:

from threading import Thread
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>")

schedule.every().day.at("17:19").do(republic, requests)
schedule.every().day.at("17:19").do(republic, requests)
schedule.every().day.at("17:19").do(republic, requests)

class SchedulerThread(Thread):
     @classmethod
     def run(cls):
         while True:
             schedule.run_pending()
             time.sleep(interval)

ScheduleThread().start()
从线程导入线程
来自未来导入打印功能
从django.shortcuts导入渲染
从django.utils导入时区
从django.http导入HttpResponse
从datetime导入datetime,timedelta
导入请求
进口时间表
导入时间
def共和国(请求):
返回HttpResponse(“成功印度斯坦”)
日程安排:每()天(“17:19”).do(共和国,请求)
日程安排:每()天(“17:19”).do(共和国,请求)
日程安排:每()天(“17:19”).do(共和国,请求)
类SchedulerThread(线程):
@类方法
def运行(cls):
尽管如此:
schedule.run_pending()
睡眠时间(间隔)
ScheduleThread().start()

最后请注意,
requests
不是一个
HttpRequest
对象,因此您不应该将函数作为视图来编写,而是作为执行某些工作的“普通”函数来编写。

它应该是
do(republic,requests)
,而不是
do(republic(request))
。我认为这里缺少的是问题的解决方案。请提供一个,我将向上投票:)当我喜欢这个“时间表。每()天。在(“17:19”).do(共和国,请求)”时,我的服务器将无法启动。它只是在终端执行系统检查时显示这一点。。。但是当我尝试喜欢这个“schedule.every().day.at”(“17:19”).do(republic(requests))”时,正如问题中提到的,服务器启动,但给出了错误。好的想法是您指定只在特定时间调用函数(每天
17:19
,因此它没有运行是正常的,而且
while 1
确实阻止了服务器启动。你应该以同步方式运行此程序。这是我项目的URL,基本上我正在尝试通过web报废获取新闻。因此,我使用调度程序每天自动获取新闻。我需要一个分析我学术项目的新闻。这是views.py文件的链接。因此,请帮助我需要做什么。类型错误“typeerror第一个参数必须可调用”已经解决,您也得到了八个。服务器在一天中指定的时间启动。at()然后,当1阻止服务器运行时。因此,正如您告诉我的那样,以异步方式运行。因此,我添加了以下COLD类ScheduleThread(对象):@classmethod def run(cls):while True:schedule.run_pending()time.sleep(10)ScheduleThread().start()但是,当我现在启动服务器时,我发现这个错误“ScheduleThread”对象没有属性“start”,您应该使用
threading
模块中的
Thread
(如标题中所指定)。当我喜欢这个“schedule.every().day.at”(“17:19”).do(共和国,请求)”然后我的服务器没有启动。它只是在终端执行系统检查时显示这一点……但是当我尝试喜欢这个“schedule.every().day.at(“17:19”).do(republic(requests))”时,正如问题中提到的,服务器启动了,但给出了那个错误。
from threading import Thread
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>")

schedule.every().day.at("17:19").do(republic, requests)
schedule.every().day.at("17:19").do(republic, requests)
schedule.every().day.at("17:19").do(republic, requests)

class SchedulerThread(Thread):
     @classmethod
     def run(cls):
         while True:
             schedule.run_pending()
             time.sleep(interval)

ScheduleThread().start()