GithubHelp home page GithubHelp logo

rmarfil3 / python3-flask-google-cloud-tasks-decorator Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 43 KB

A decorator for Python 3 + Flask that allows a function to run in the background via Google Cloud Tasks.

License: MIT License

Python 100.00%
flask python3 google-cloud google-cloud-platform decorator tasks google-cloud-tasks

python3-flask-google-cloud-tasks-decorator's Introduction

Google Cloud Tasks Decorator for Python 3 + Flask

A decorator for Python 3 + Flask that allows a function to run in the background via Google Cloud Tasks.

Usage

@decorator.push_queue
def send_email(subject, to, html):
    msg = Message(subject, recipients=to)
    msg.html = html
    msg.send()


# 1. Run in the background
send_email.delay("Hello", "[email protected]", "Hello, world!")

# 2. Use function normally
send_email("Hello", "[email protected]", "Hello, world!")

Dependencies

Setup

Add this to your Flask application's init file

from google.cloud import tasks_v2

project = os.environ.get('GOOGLE_CLOUD_PROJECT')  # my-appengine-project
location = os.environ.get('CLOUD_TASK_LOCATION')  # us-central1
queue = os.environ.get('CLOUD_TASK_QUEUE')  # default

cloud_task_client = None
cloud_task_parent = None
if os.environ.get('GAE_ENV') == 'standard':
    cloud_task_client = tasks_v2.CloudTasksClient()
    cloud_task_parent = cloud_task_client.queue_path(project, location, queue)

Notes

The code is set to run in the background only when deployed in Google AppEngine environment. Please see delay() function in decorator.py, particularly line 36 if os.environ.get('GAE_ENV') == 'standard'.

If code is unmodified and ran outside Google AppEngine environment, .delay() will simply call the function directly.

python3-flask-google-cloud-tasks-decorator's People

Contributors

rmarfil3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.