Cython for a Django app: would it work?

三世轮回 提交于 2019-12-18 13:17:46

问题


Would compiling with cython work with a python 3.4 Django app, or could it be made to work without a huge amount of effort?

This answer - https://stackoverflow.com/a/7347168/805141 - to a question about protecting python code prompted me to ask this question.

A similar question has been asked previously but with regards to improving performance: Using Cython with Django. Does it make sense?


回答1:


Yes, we have done it. But it point of consistent pain.

We make a commercial product which is installed on the customer premise to manage their Genesys power contact center. The core of the application is written in Django and we wanted to protect (limit) the code from inspection.

There is a speed improvement from running in native python but it is not a considerable difference. The improvement depends on the type of task, sometimes up to 30% sometimes minimal.

We run into issues from time to time where something works in Python but then it does not in Cython. I would not recommend this path unless you have a really good motivation.

Currently version runs on Python 3.5 with Django 1.11




回答2:


I know It is too late to answer. Even though It might help. I have created a setup.py file in the project home directory.

from distutils.core import setup
from Cython.Build import cythonize
fileSet = set()
fileSet.add("app1/file1.py")
fileSet.add("app2/file2.py")
fileSet.add("app3/file3.py")
setup(
   ext_modules=cythonize(fileSet)
)

Scan your app directories and add files to the fileSet whatever you want to compile. file1.py, file2.py and file3.py are just examples only.

Finally, just run the setup.py file as below

python setup.py build_ext --inplace 

Then Cython stats compiling each file and makes it .so file. Example: app1/file1.so app2/file2.so app3/file3.so

These files are shared object files and you cannot interpret manually. Delete all .py and .pyc files. And then run your project as

python manage.py runserver

or you can host these binaries in your production server. I tried on NGINX, uWSGI.

Good Luck.



来源:https://stackoverflow.com/questions/32577864/cython-for-a-django-app-would-it-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!