4

I need to execute some python machine learning code and was hoping to execute it from a node process within a Google Cloud Function.

I want to use node because I have some firebase admin tasks I want to run afterwards. I also have a deployment workflow with node that I'm content with.

Is this possible? If so, can you point me to an example?

Matthew Chung
  • 1,322
  • 1
  • 19
  • 31

1 Answers1

3

Yes, this would be possible, you probably need to use http://www.pyinstaller.org/ to package the python code. Have a look at my python library for actually writing your functions in python to do it, or just copy paste whatever parts you need to do it yourself: https://github.com/MartinSahlen/cloud-functions-python/

martin.code
  • 1,291
  • 9
  • 12
  • 1
    Can you elaborate a bit on how this works. Are you compiling Python into JS or are you co-locating the Python stuff in a container where the cloud function's JS can call it? I'm new to cloud functions and cannot yet imagine how it works, given the technology's current out-of-the-box [limitation to JS](https://cloud.google.com/functions/docs/writing/). – Drux Dec 27 '17 at 06:22
  • So what is happening is that pyinstaller is used to package the python code into a self-executable unit. How that works is probably by embedding the necessary parts of the python runtime but exactly how that works is not known to me - but pyinstaller is open source and well documented so information should be there. After packaging the python code, there is a node script that executes the function sending and recieving data from the python executable using stdin / stdout. Again, please have a look at the source code in githhub - it's all there. – martin.code Jan 02 '18 at 15:22
  • 1
    Thx for you response, I get the picture now. – Drux Jan 02 '18 at 19:55
  • I have packaged my python code into the self-executable unit..which creates a folder containing an executable and a lot of other stuff. I tried and that executable works sufficiently on my local machine. But being new to Firebase, I have no idea how to upload this and run it.I want to use it via spawn. Need help..how is that done? – Aakash Choubey Feb 18 '18 at 20:36
  • @AakashChoubey this is what I answered above. You should check out the source code which does exactly this. keep in mind, this is not set up to work with firebase functions, only google cloud functions. – martin.code Feb 19 '18 at 20:51
  • @unacast.martin I just mailed to your gmailID. My implementation is very similar to the original question here. You never mentioned a pyinstaller in your git. I have a python code needs to run on cloud-function. I need more guidance – Santhosh Mar 10 '18 at 07:38
  • Yes, it is mentioned briefly in the end of the readme, the last sections about adding support for packages that doesn’t work / exist. Secondly, looking at the source code you can see it clearly. Also keep in mind this is a “alpha” level project and open source so if something is not working, feel welcome to dive in to the code and contribute. – martin.code Mar 11 '18 at 11:31