按计划在Google App Engine上运行python脚本 [英] Run a python script on schedule on Google App Engine

查看:138
本文介绍了按计划在Google App Engine上运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一款优秀的撒玛利亚人,它可以提供一个非常基本的框架来使用Google App Engine运行python脚本。我已经阅读了文档,检查了相关的SO问题,但是我迷失了WebApp格式。我要做的就是每周运行6次运行一个接受参数的python脚本或几个python脚本,以监听网站中的更改,然后将其发布到Firestore。


我了解cron格式和大多数配置文件。我一直在研究如何为项目安排文件以及url的工作方式。


我要问的是一个关于如何有效运行python脚本的非常基本的示例。


I'm looking for a good samaritan that can provide with a very basic skeleton to run a python script using Google App Engine. I have read the documentation, check on related SO questions but I'm lost with the WebApp format. All I want to do is run one python script which accepts arguments or several python scripts, 6 times a week to listen to for changes in a website and then post them to Firestore.

I understand the cron format and most of the configurations files. I'm stuck on how to arrange my files for the project, and how the url's works.

All I'm asking is a very basic sample on how to effectively run the python scripts. This is by far the best resource that I have found, but I can't really understand what is going on with this code from that site:

`#!/usr/bin/python
# -*- coding: utf-8 -*- 
from __future__ import unicode_literals   
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.ext import db   
import feedparser  
import time   

class Item(db.Model): 
    title = db.StringProperty(required=False)
    link = db.StringProperty(required=False)
    date = db.StringProperty(required=False)   class Scrawler(webapp.RequestHandler):
    
    def get(self):
        self.read_feed()      
        self.response.out.write(self.print_items())
        
    def read_feed(self):
        
        feeds = feedparser.parse( "http://www.techrepublic.com/search?t=14&o=1&mode=rss" )
        
        for feed in feeds[ "items" ]:
            query = Item.gql("WHERE link = :1", feed[ "link" ])
            if(query.count() == 0):
                item = Item()
                item.title = feed[ "title" ]
                item.link = feed[ "link" ]
                item.date = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(time.time()))
                item.put()
    
    def print_items(self):
        s = "All items:<br>"
        for item in Item.all():
            s += item.date + " - <a href='" + item.link + "'>" + item.title + "</a><br>"
        return s   application = webapp.WSGIApplication([('/', Scrawler)], debug=True)   def main():
    run_wsgi_app(application)   if __name__ == "__main__":
    main() `

This is the python script I tried to run for testing only, using python3.7:

import sys
from datetime import datetime

import firebase_admin
from firebase_admin import firestore

app = firebase_admin.initialize_app()
db = firestore.client()


def hello_firestore(user_name):
    db.collection('firestore_test').document('test').set({
        'time': str(datetime.now()),
        'user_name': user_name
    })


if __name__ == "__main__":
    try:
        user_name = sys.argv[1]
    except:
        print('Error with the argument', file=sys.stderr)
    try:
        hello_firestore(user_name)
    except:
        print('Error accessing the database', file=sys.stderr)
        sys.exit(0)

For what I understand I have to use Flask or something similar to make it work, but I don't really understand how it works, all I'm asking is a small sample and and brief explanation, and from there I'll add two and two.

Best Regards

解决方案

Finally my kids will love me again. Turns out I was looking at the wrong GCP resource, as @Dan_Cornilescu pointed out that might be a way to do it, but the easiest way to do it is "Cloud Functions" in Conjunction with "Cloud Scheduler" and I found it just by mere chance.

This Article was the very first one that mentioned it, at the moment I passed on it because the autor again uses a web app to illustrate the case, for my needs and lack of technical argot, I just couldn't dig it. But it is really as simple as it was supposed to be, in your Google Cloud Console:

  1. Go to the Functions Section
  2. Choose as trigger "Cloud Pub/Sub"
  3. Add/Choose a topic
  4. Select your runtime(Python3.7 of course)
  5. Select function to execute
  6. Create
  7. Make sure you fill the "requirements.txt" file on the next tab
  8. Go to Cloud Scheduler section of GCP and Create a job(cron job)
  9. Choose as target: "Pub/Sub"
  10. Enter the topic you chose for your function
  11. If you want to send arguments for your functions, use the payload for that purpose.

To use an argument or arguments for your Python function you want to use the payload and using the following from their initial function:

pubsub_message = base64.b64decode(event['data']).decode('utf-8')

This pubsub_message you can use it as an argument for your python functions.

And that's all folks, easy, super easy, at the end I think is just the same of a GAE without the visual page, just what I was needed, I knew there's gotta be a better way.

EDIT: The article I mention here describe how to use gcloud to upload your function(s) directly from your computer.

这篇关于按计划在Google App Engine上运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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