Home > OS >  Problem with @response.call_on_close in Google Cloud Run
Problem with @response.call_on_close in Google Cloud Run

Time:10-19

I'm trying to write a script that uses @response.call_on_close on Google Cloud Run, to send an inmediate return to the invoker and do some processing after that, so that the invoker doesn't keep waitng.

The script involves the use of Selenium, and it works fine in a local Cloud Run, but when deployed on the actual Cloud I get a "devToolsActivePort file doesn't exist" error.

When I comment out all of the @response.call_on_close part and invoke it directly, it also works fine, so it has nothing to do with Selenium, there must be a problem with the decorator part but I can't figure it out.

This is the code I'm using to make the call:

from flask import Flask, request
from scraper import scrap


app = Flask(__name__)

 @app.after_request
 def response_processor(response):

     request_json = request.get_json()
     keyword = request_json['keyword']
     tztimezone = request_json['tztimezone']

    @response.call_on_close
    def process_after_request():
         scrap(keyword, topic, tztimezone)
    return response

@app.route("/", methods=['GET', 'POST'])
def main():
    if request.method != 'POST':
        return 'Only POST requests are accepted', 405
    return ''

Any help will be greatly appreciated.

Thanks!

CodePudding user response:

You cannot perform CPU processing after you return a response to the client.

Google Cloud Run considers the service request is complete once you return a response. The CPU will be put to sleep for your container until the next request.

This link will help:

Lifecycle of a container on Cloud Run

CodePudding user response:

Cloud Run just got a new feature: Always on cpu to disable the CPU throttling.

However, you want pays the processing time ONLY when the request is processed, you will pay for the instance until it is offloaded (about 15 minutes after the latest request received).

But, you have a CPU and memory cost discount (25% and 20%)


Be careful:

If your background job takes more than 15 minutes and the instance didn't receive new request, it will be offloaded and your job killed. This feature is designed to continue the process during several seconds after the request end.

Cloud Run team doesn't guaranty the 15 minutes (commonly observed) especially if the platform get an heavy demand on new Cloud Run instances.

In this case, you can combine this feature with the min instance feature. We can discuss further the different tradeoff of this design.

  • Related