Code speedup with Cython even when included in custom runtime, not happening

205 views
Skip to first unread message

Vipluv Aga

unread,
Jun 11, 2020, 5:50:41 PM6/11/20
to Google App Engine
I am running a flask app accessed by endpoints  on google app engine flex

There are certain modules that use shapely package of python. The exact same app and inputs on my local machine for a certain calculation take 36 seconds say, while once deployed on GAE it takes above 9 to 10 minutes.
First I thought that locally often the IDE converts packages to cython modules for speedups. I tried doing the same by converting the flex engine to custom runtime, and running a Dockerfile to build the Cython packages. 

My Dockerfile is as below

LABEL python_version=python3.6
RUN virtualenv --no-download /env -p python3.6

# Set virtualenv environment variables. This is equivalent to running
# source /env/bin/activate
ENV VIRTUAL_ENV /env
ENV PATH /env/bin:$PATH

ADD requirements.txt /app/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
ADD . /app/

RUN python cythonsetup.py build_ext --inplace
CMD exec gunicorn -t 600 -c gunicorn.conf.py -b :$PORT main:app


My cythonsetup.py to build and include the cython builds

from setuptools import setup,Extension
#from Cython.Build import cythonize
from Cython.Distutils import build_ext

ext_modules = [
    Extension("addonModules.GridMakerCy", ["addonModules/GridMaker.py"], ),
    Extension("addonModules.GeoCoordtoMetersCy", ["addonModules/GeoCoordtoMeters.py"],)]

for e in ext_modules: 
    e.cython_directives = {"language_level": "3"}

setup(name='GridMaker',
      cmdclass = {'build_ext': build_ext},
      ext_modules = ext_modules,
      
      )



And i import the built cythons correctly (I confirmed the code gave an answer as I named the cython modules differently from python modules), however the calc time didnt change -- locally it takes very little time (even after including datastore and cloud storage read/writes--- which take longer on a local machine with gcloud clients) but in the app engine it takes very very long. 

I logged into the GAE instance itself, and
 after 
docker exec -it gaeapp /bin/bash

i confirmed that cython is installed. Then why is the run so slow?





Manpreet Sidhu (Google Cloud Support)

unread,
Jun 12, 2020, 10:34:45 PM6/12/20
to Google App Engine
Thanks for reaching out. When Cython is slower, it can probably be pointed to type conversions. Here's a Stack Overflow post with a code speed test between Python and Cython.

These types of questions are best suited for Stack Overflow. Google Groups are best resources for product discussions and getting information like service status updates and release notes, and ranging from book recommendations to creative shortcuts. You can get more details regarding our community support here.
Reply all
Reply to author
Forward
0 new messages