每次我停止服务器时,Python作业调度模块出错

时间:2016-08-14 13:03:03

标签: python django

我的Django项目中的models.py文件包含基于following tutorial的代码:

import schedule
import time

def job():
        print("I'm working...")

schedule.every(3).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

每次运行应用程序时,我都会得到以下输出:

"C:\Program Files (x86)\JetBrains\PyCharm 2016.1\bin\runnerw.exe" C:\Python27\python.exe D:/blogpodapi/manage.py runserver 8000
I'm working...
I'm working...

它按预期工作,但每当我关闭应用程序时,我得到以下输出:

Traceback (most recent call last):
  File "D:/blogpodapi/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 302, in execute
    settings.INSTALLED_APPS
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 55, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "D:\blogpodapi\blogpodapi\__init__.py", line 13, in <module>
    time.sleep(1)
KeyboardInterrupt

Process finished with exit code 1

除此之外,我的服务器无法启动......因为当我删除上述代码时,我得到以下输出:

"C:\Program Files (x86)\JetBrains\PyCharm 2016.1\bin\runnerw.exe" C:\Python27\python.exe D:/blogpodapi/manage.py runserver 8000
Performing system checks...

System check identified no issues (0 silenced).
August 14, 2016 - 14:00:32
Django version 1.9.6, using settings 'blogpodapi.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

我不确定导致此错误的原因。当调度程序就位时,服务器不会启动,每次我尝试在服务器到位时停止服务器时都会出现错误。理想情况下,我希望服务器按预期启动,并在此之后开始运行计划任务。关于我如何解决这个问题的任何建议?

可以在此处找到整个models.py文件:http://pastebin.com/0DwQWqM8

1 个答案:

答案 0 :(得分:1)

似乎没有任何问题。

您发布的Traceback报告了一个KeyboardInterrupt,它通常用于强制退出应用程序,因此完全正常。

服务器似乎也已启动。 您是否尝试在服务器运行时访问http://127.0.0.1:8000/? 最后一行只是告诉您如果要停止服务器,请在命令行中使用CTRL-C。