Google Cloud FunctionsでPythonを使ってみる
Public Betaになりましたが、Pythonのサポートはまだのようなので無理やり使ってみます。
準備
Node.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);
}
};
print('hello functions!')
この2つのファイルをZipで圧縮し、Cloud FunctionsのConsole画面でアップロードします。
これでdeployは完了、HTTP Triggerなので記載のURLをブラウザで開けばPythonが出力 'hello functions!'が表示されます。
Python環境
Pythonを使えることがわかったので、もうちょっと詳細を調べてみます。
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')
とりあえず気になったライブラリの有無を列挙。 ・・・全滅でした
3rd-party libは準備されていないようです。
他ざっと調べた内容
Python version | 2.7.9 |
OS | Debian系 8.7 |
pip | N/A |
easy_install | N/A |
File System | read only |
File Systemがread onlyなので、実行時に何かをインストールして使うのは無理そうです。
ですがdeploy時のZipに含まれるファイル形式に制限はなさそうなので、
バイナリなど予め入れておけば実行できそうです。
参考
GooglerがGoで同じことをやっています。そちらの方が参考になるかも。
https://github.com/kelseyhightower/google-cloud-functions-go
最後に
Pythonを使うのは現状かなり手間ですが、通常通りNode.jsを使うぶんには非常に簡便に機能を実行できる環境だと思います。Console上でサクッと書いて、すぐにdeployできるのはとても魅力です。
Author And Source
この問題について(Google Cloud FunctionsでPythonを使ってみる), 我々は、より多くの情報をここで見つけました https://qiita.com/hayatoy/items/67406fb1b30db0d21c12著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .