Home > Net >  Numpy error on Google Cloud Functions which does not happen locally
Numpy error on Google Cloud Functions which does not happen locally

Time:01-18

Below google cloud function runs fine locally but fails when deployed on the cloud.

from flask import escape
import functions_framework
import numpy as np

@functions_framework.http
def test_func(request):
    np_array = np.zeros(172128250, dtype=np.complex64)
    print('np_array shape: ', np_array.shape)
    abs_array = np.abs(np_array)
    print('abs_array shape: ', abs_array.shape)
    return 'Hello World'

Python versions tried: 3.7, 3.8, and 3.9.

Timeout: 540s

Memory: 8GB

np_array is successfully created but it fails to compute abs. Function prints first log np_array shape: (172128250,) but fails to print the second log. The functions exits abruptly printing Function execution took 2408 ms, finished with status: 'connection error'. It works for all the sizes locally, but on the cloud, it specifically fails for the NumPy array of the following 10 sizes [172128241, 172128242, ..., 172128250].

CodePudding user response:

Your execution environment is sandboxed and you can't get some CPU features (for more details, have a look to gVisor project).

To solve that, I can recommend you to switch to Cloud Run (simply wrap your code in a web server, see my old article here) and use execution runtime gen2 (still in preview)

CodePudding user response:

Found that the error occurs with Numpy version: 1.19.4 on the cloud. After using 1.21.5 on the cloud, the error goes away. It's still strange why it works locally with 1.19.4 but fails on the cloud.

  •  Tags:  
  • Related