Try using Python with Google Cloud Functions

It became Public Beta, but it seems that Python support is not yet available, so I will try to use it.

Preparation

I haven't written Node.js, but I wrote Gugu like this.

index.js


const spawnSync = require('child_process').spawnSync;

exports.helloWorld = function helloWorld(req, res) {
  
  result = spawnSync('python', ['./inspect.py'], {
    stdio: 'pipe',
  });
  
  if (result.stdout){
    res.status(200).send(result.stdout);
  }else if (result.stderr){
    res.status(200).send(result.stderr);
  }
};

inspect.py


print('hello functions!')

Compress these two files with Zip and upload them on the Console screen of Cloud Functions.

Cloud_Functions_-_Test_fx_lab.png

This completes deploy, HTTP Trigger, so if you open the URL in your browser, Python will output'hello functions!'.

Python environment

Now that I know I can use Python, let's look into a little more detail.

inspect.py


try:
    import tensorflow as tf
    print('tensorflow %s' % tf.__version__)
except:
    print('tensorflow n/a')

try:
    import sklearn
    print('sklearn %s' % sklearn.__version__)
except:
    print('sklearn n/a')

try:
    import numpy as np
    print('numpy %s' % np.__version__)
except:
    print('numpy n/a')

try:
    import scipy as sp
    print('scipy %s' % sp.__version__)
except:
    print('scipy n/a')

List the existence of the library that I was interested in for the time being. ・ ・ ・ ** It was annihilated ** 3rd-party lib doesn't seem to be prepared.

What I did a quick look at

Python version 2.7.9
OS Debian series 8.7
pip N/A
easy_install N/A
File System read only

Since the File System is read only, it seems impossible to install and use something at runtime. However, there seems to be no limit to the file format included in Zip at the time of deploy, so It seems that it can be executed if you put the binary etc. in advance.

reference

Googler is doing the same with Go. That may be more helpful. https://github.com/kelseyhightower/google-cloud-functions-go

Finally

Currently, using Python is quite a hassle, but I think that it is an environment where you can execute functions very easily as you use Node.js as usual. It is very attractive to be able to write quickly on the Console and deploy immediately.

Recommended Posts

Try using Python with Google Cloud Functions
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Try mathematical formulas using Σ with python
[GCP] Operate Google Cloud Storage with Python
How to connect to Cloud Firestore from Google Cloud Functions with python code
Try scraping with Python.
[Azure] Try using Azure Functions
Try using Tweepy [Python2.7]
Try it with Word Cloud Japanese Python JupyterLab.
Try running Google Chrome with Python and Selenium
Curry arbitrary functions with Python ....
Getting Started with Python Functions
[S3] CRUD with S3 using Python [Python]
[Python] Try using Tkinter's canvas
Try Google Mock with C
Using Quaternion with Python ~ numpy-quaternion ~
Try using matplotlib with PyCharm
Easily try Amazon EMR / Cloud Dataproc with Python [mrjob]
Try Python output with Haxe 3.2
Try using Kubernetes Client -Python-
Study Python with Google Colaboratory
[Python] Using OpenCV with Python (Basic)
Try projective transformation of images using OpenCV with Python
Easy way to scrape with python using Google Colab
Try running Python with Try Jupyter
Speech transcription procedure using Python and Google Cloud Speech API
Run Google Cloud Functions locally with Cloud Native Build packs
Access Google Drive with Python
Try face recognition with Python
Try OpenCV with Google Colaboratory
Try using folium with anaconda
Using OpenCV with Python @Mac
Send using Python with Gmail
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Cloud Functions to resize images using OpenCV with Cloud Storage triggers
Try to determine food photos using Google Cloud Vision API
Touch NoSQL with Python using the Oracle NoSQL Database Cloud Simulator
Try encryption / decryption using OpenSSL key with Python3 pow function
This and that for using Step Functions with CDK + Python
Try to implement linear regression using Pytorch with Google Colaboratory
Play with YouTube Data API v3 using Google API Python Client
Complement python with emacs using company-jedi
Print PDF using Google Cloud Print. (GoogleAPI)
Harmonic mean with Python Harmonic mean (using SciPy)
Try scraping with Python + Beautiful Soup
[Package cloud] Manage python packages with package cloud
Azure Functions: Try Durable Functions for Python
[Python] Using OpenCV with Python (Image Filtering)
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
Try to operate Facebook with Python
[Python3] Google translate google translate without using api
[Python] Using OpenCV with Python (Edge Detection)
Try singular value decomposition with Python
Try using Pleasant's API (python / FastAPI)
Run XGBoost with Cloud Dataflow (Python)
Using global variables in python functions
Using Cloud Storage from Python3 (Introduction)
10 functions of "language with battery" python
Try face recognition with python + OpenCV
Try using Python argparse's action API