繁体   English   中英

在GAE python中运行cron作业有问题

[英]Trouble with running cron job in GAE python

因此,我做了一项计划工作,每隔1分钟用新故事更新我的新闻聚合器应用程序一次。 我应该说我是一位完全的cron职位新手,在运行GAE方面经验非常有限。

这是我的文件夹结构:

  • news.py
  • index.yaml
  • feedparser.py
  • cron.yaml
  • app.yaml
  • 范本
  • 静态的

这是news.py

feed = ['https://news.google.co.in/news/section?cf=all&pz=1&ned=in&topic=e&ict=ln&output=rss&num=10']

feedlist = []

def render_str(template, **params):
    t = jinja_env.get_template(template)
    return t.render(params)

class CronTask(webapp2.RequestHandler):
    def get(self):
        self.redirect('/entertainment')

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.write(render_str('mainpage.html'))

class Entertainment(webapp2.RequestHandler):
    def get(self):
        rssfeed = feedparser.parse(feed)
        for news in rssfeed.entries:
            new_entry = {'title': news.title, 'url': news.link, 'publisheddate': news.published}
            feedlist.append(new_entry)
        self.redirect('/1/display')

class Display(webapp2.RequestHandler):
    def get(self, page_no):
        is_this_last = False
        list_to_be_displayed_here = feedlist[(int(page_no)-1)*5:int(page_no)*5]
        try:
            is_last = feedlist[int(page_no)*5]
        except:
            is_this_last = True
        self.response.write(render_str('/display.html', page_no=page_no, feedlist=list_to_be_displayed_here, is_this_last=is_this_last))

app = webapp2.WSGIApplication([('/', MainPage),
                           ('/entertainment', Entertainment),
                           ('/([0-9]+)/display', Display),
                           ('/crontask', CronTask)
                            ], debug = True)

我假设这是应该设置cron.yaml方式:

cron:
- description: periodic update of news
  url: /crontask
  target: beta
  schedule: every 1 minute

这是app.yaml:

application: encoded-alpha-139800
version: 1
runtime: python27
api_version: 1 
threadsafe: true

handlers:
- url: /static
  static_dir: static

- url: /crontask
  script: news.py

- url: /.*
  script: news.app

libraries:
- name: jinja2
  version: latest

display.html只是显示提要的信息,并且由于我不知道如何实现cursor()方法,因此我实现了对Display get()中看到的基本分页, feedlist我的切片进行了切片。

当我运行news.py ,我得到以下回溯:

  File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 83, in <module>
_run_file(__file__, globals())
  File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 79, in _run_file
execfile(_PATHS.script_file(script_name), globals_)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 1040, in <module>
main()
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 1033, in main
dev_server.start(options)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 758, in start
options.config_paths, options.app_id)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\application_configuration.py", line 831, in __init__
module_configuration = ModuleConfiguration(config_path, app_id)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\application_configuration.py", line 127, in __init__
self._config_path)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\application_configuration.py", line 424, in _parse_configuration
config, files = appinfo_includes.ParseAndReturnIncludePaths(f)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\appinfo_includes.py", line 82, in ParseAndReturnIncludePaths
appyaml = appinfo.LoadSingleAppInfo(appinfo_file)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\appinfo.py", line 2190, in LoadSingleAppInfo

listener.Parse(app_info)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\yaml_listener.py", line 227, in Parse
self._HandleEvents(self._GenerateEventParameters(stream, loader_class))
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\yaml_listener.py", line 178, in _HandleEvents
raise yaml_errors.EventError(e, event_object)
google.appengine.api.yaml_errors.EventError: threadsafe cannot be enabled with CGI handler: news.py
  in "C:\Users\IBM_ADMIN\Downloads\7c\NewsAggregatorGAE\app.yaml", line 19, column 18

是因为我试图通过cron作业运行几乎整个应用程序吗? 还是我的设置或整个设置有问题?

问题是您在app.yaml文件中指示处理程序为CGI应用程序:

script: news.py

请求处理程序

当App Engine收到针对您的应用程序的Web请求时,它将调用与该URL相对应的处理程序脚本,如应用程序的app.yaml配置文件中所述。 Python 2.7运行时支持WSGI标准和CGI标准,以实现向后兼容。 首选WSGI,没有它,Python 2.7的某些功能将无法使用。 应用程序脚本处理程序的配置确定是使用WSGI还是CGI处理请求。

...

如果您将应用程序标记为线程安全,则将启用并发请求,这意味着App Engine可以并行向每个Web服务器分派多个请求。 为此,请在app.yaml中设置threadsafe:true。 如果任何脚本处理程序使用CGI,则并发请求不可用。

只要使其成为WSGI应用程序,该错误就会消失:

script: news.app

请记住,GAE cron服务不过是根据已配置的计划向配置的URL进行GET请求的生成器。 使用Cron for Python计划任务

通过App Engine Cron服务,您可以配置按计划的任务,这些任务在定义的时间或固定的时间间隔运行。 这些任务通常称为cron作业。 这些cron作业由App Engine Cron服务自动触发。

...

cron作业在一天的给定时间使用HTTP GET请求调用URL。 cron作业请求受与推送任务队列相同的限制。

您的应用如何执行cron作业实际上归结为它如何处理这些请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM