How to handle threads in Python functions in Google Cloud Functions?

How to handle threads in Python functions in Google Cloud Functions?

By : i12bsilly
Date : October 18 2020, 06:10 PM
I wish this helpful for you If I am understanding correctly, we have an original REST caller (Facebook) that requires a response in 20 seconds or less. However, your function that is handling the request wishes to call a service that may take longer than 20 seconds to respond. One possible solution is to realize that the the response sent by the original Cloud Function can be returned even before the code logic in that Cloud Function completes. Assuming you are coding in Node.js, you are passed a response object to the Cloud Function. You can call send() and end() on this object within the Cloud Function body and then carry on and do your other long-running work. If you are wanting to return a success criteria on the overall process to your original caller, it sounds like we have conflicting stories. The contract seems to say that the response must be 20 seconds or less while the work may take more than 20 seconds. As such, you can't sensibly return a response indicating full outcome in all cases.
If you are writing your application in Python, the story is not so easy. Cloud Functions leveraging Python is based of flask and with flask, a Python function is called (that you supply). The data returned from this function is the data that is returned to the caller. If we want to return early and THEN do additional work, one recipe is described here. Unfortunately, it doesn't look like we can avail ourselves of this story because Cloud Functions owns and manages the Flask environment and we don't have a legal way to add additional handlers.
code :

Share : facebook icon twitter icon
How do i handle secrets in Google Cloud Functions?

How do i handle secrets in Google Cloud Functions?

By : Дима Ерохин
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Since making my comment, I've found a relatively simple way to do this - provide a config .json file. Here's an example I hacked together based on their Slack function example:
config.json file in the same directory as index.js:
code :
  "foo": "bar"
const config = require('./config.json');

exports.envTest = (req, res) => {
How to handle backpressure using google cloud functions

How to handle backpressure using google cloud functions

By : Alexander
Date : March 29 2020, 07:55 AM
will be helpful for those in need This functionality is not available for Google Cloud Functions. Instead, since you are asking to handle the pace at which the system will open concurrent tasks, Task Queues is the solution.
Firebase/Google cloud functions - is it possible to hook into cloud functions error event?

Firebase/Google cloud functions - is it possible to hook into cloud functions error event?

By : Mike Lane
Date : March 29 2020, 07:55 AM
This might help you You cannot modify how Node.js handles errors, as Google CFs don't allow it [1].
You can check here how the execution environment handles errors.
Read a CSV from Google Cloud Storage using Google Cloud Functions in Python script

Read a CSV from Google Cloud Storage using Google Cloud Functions in Python script

By : Ankit
Date : March 29 2020, 07:55 AM
hope this fix your issue I'm new in GCP and I'm trying to do a simple API with Cloud Functions. This API needs to read a CSV from Google Cloud Storage bucket and return a JSON. To do this, in my local I can run normally, open a file. , This is the code that worked for me:
code :
from google.cloud import storage 
import csv

client = storage.Client()

bucket = client.get_bucket('source')

blob = bucket.blob('file')

dest_file = '/tmp/file.csv'

dict = {}
total = 0

 with open(dest_file) as fh:
    # assuming your csv is del by comma
    rd = csv.DictReader(fh, delimiter=',')
    for row in rd:
         date = datetime.datetime.strptime(row['date'], '%Y-%m-%d')
                #check if date falls within requested range
         if date >= datetime.datetime.strptime(request.args['start_date']) and date <= datetime.datetime.strptime(request.args['end_date']):
                total = total + float(row['total'])
         dict['total_faturado'] = total 
How to write a list to a file in Google Cloud Storage using Google Cloud Functions with Python

How to write a list to a file in Google Cloud Storage using Google Cloud Functions with Python

By : user3597586
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I am trying to write members of a list into a file in a bucket in Cloud Storage using Cloud Functions. , I managed to get it done with the following python code:
code :
import datetime
import logging
import os
import sqlalchemy
from google.cloud import storage
import pandas as pd

# Remember - storing secrets in plaintext is potentially unsafe. Consider using
# something like https://cloud.google.com/kms/ to help keep secrets secret.
db_user = "<DB_USER>"#os.environ.get("DB_USER")
db_pass = "<DB_PASS>"#os.environ.get("DB_PASS")
db_name = "<DB_NAME>"#os.environ.get("DB_NAME")
cloud_sql_connection_name = "<Cloud SQL Instance Connection Name>"#os.environ.get("CLOUD_SQL_CONNECTION_NAME")
logger = logging.getLogger()

# [START cloud_sql_postgres_sqlalchemy_create]
db = sqlalchemy.create_engine(
    # Equivalent URL:
    # postgres+pg8000://<db_user>:<db_pass>@/<db_name>?unix_sock=/cloudsql/<cloud_sql_instance_name>/.s.PGSQL.5432
            'unix_sock': '/cloudsql/{}/.s.PGSQL.5432'.format(
    # ... Specify additional properties here.
    pool_timeout=30,  # 30 seconds
    pool_recycle=1800,  # 30 minutes

def read_source_data(request):
    bucket_name = <YOUR_BUCKET_NAME>
    folder_name = "sample_files"
    file_name = "test.txt"

    with db.connect() as conn:
        sales_records = conn.execute(
            "SELECT * FROM sales;"

    if len(sales_records) > 0:
        #for val in sales_records:
        df = pd.DataFrame(sales_records)
        df.columns = sales_records[0].keys()
        create_file(bucket_name, "sample_files/test.txt", df)
        return "Done!"
        return "Nothing!"

def create_file(bucketname, path, records_read):
  storage_client = storage.Client()
  bucket = storage_client.get_bucket(bucketname)
  blob = storage.Blob(

  content = records_read.to_csv(index=False)#'\n'.join(map(str, records_read))

gcloud deployment-manager deployments  create
Related Posts Related Posts :
  • Unit Test Won't Run Tests
  • Use two for loops simultaneously
  • can we display glass bar chart in python with google app engine
  • Scapy install issues. Nothing seems to actually be installed?
  • Why do people write the #!/usr/bin/env python shebang on the first line of a Python script?
  • What does s() mean?
  • ROC AUC value is 0
  • Why is this the value?
  • Best practices for logging in django project
  • Is there a python openid apps-discovery library to get appengine apps onto the apps marketplace
  • Cannot fetch a web site with python urllib.urlopen() or any web browser other than Shiretoko
  • Similar to ``tabnanny``, how can I check that all the python code is using 4 spaces as an indent?
  • Python: object identity question?
  • Multiple For loops, print else only once if condition is not met
  • Select one item from Series and keep the index
  • __repr__ method appears can't be invoked automatically for Exception class
  • Problem with list value (ValueError) in python 3
  • How to get TouchSensor nested under joint in Webots (Python API)
  • How to specify kernel while executing a Jupyter notebook using Papermill's Python client?
  • How to hide password in Database Connection?
  • How to get a list of dictionaries from the following code?
  • 'How to find out noun to which pronoun is referring to' in python
  • Removing a character (^) from each row of panda Dataframe and get unique words in each row
  • Changing a static variable of inherited classes
  • Django Query result comparison with if statement
  • Python: how to merge two dataframe based only on different columns?
  • Filter data by last 3 months and by ID
  • Inplace arithmetic operation versus normal arithmetic operation in PyTorch Tensors
  • How can I add custom signs to spaCy's punctuation functionality?
  • Ensure positive difference of two numbers
  • i keep getting an error that my list index is out of range
  • Is there a way to create gantt charts in python?
  • How to view network weights and bias during training
  • How can I force SAS to wait for a command to fully execute?
  • Remove all occurences of a value from a nested dictionary
  • How to ensure secure randomization for python password generator
  • Amazon SageMaker deploying from model artifacts - what object do we load from archive?
  • [] parameter or input used in sum() function - what is it doing?
  • Outlook email text formatting from Python application
  • Python 3 - comparing enums against hex value
  • Elegant way to check if a float is between two numbers in Python?
  • Understanding return [0,size-1][nums[0]<nums[size-1]] in Python
  • How do I make this script that heats up my CPU safe
  • RegEx for matching capital letters and numbers
  • What is differnces between array[0] and array[0:1] in Python?
  • How to run both items in scrapy function?
  • How to count the number of sequences of n numbers where no two adjacent numbers are the same?
  • Is there a more efficient way to re-write multi if-else statement
  • ValueError: Error when checking target: expected dense_3 to have shape (1000,) but got array with shape (1,)
  • SytanxError: Invalid Sytax
  • Setting debug = False makes the Django app crash with the following error, how to fix it?
  • How to get the average of many lists embedded within each other?
  • Paramiko with subprocess
  • 2D table conversion for example: y = f(x1,x2) => x1 = f(y, x2)
  • Return a literal string of a tuple in python
  • How to split a Column when you have same values?
  • How to perform str.strip in dataframe and save it with inplace=true?
  • why zip(*k) can't work when k is a iterator?
  • How to get list as an input from command line python?
  • Is Tensorflow Dataset.from_generator deprecated in tensorflow 2.0 ? It throws tf.py_func deprecation error
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk