简体   繁体   English

用于 Django 应用程序的 Cython:它会工作吗?

[英]Cython for a Django app: would it work?

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?使用 cython 进行编译是否可以与 python 3.4 Django 应用程序一起使用,或者它是否可以在不付出大量努力的情况下工作?

This answer - https://stackoverflow.com/a/7347168/805141 - to a question about protecting python code prompted me to ask this question.这个答案 - https://stackoverflow.com/a/7347168/805141 - 关于保护 python 代码的问题促使我问这个问题。

A similar question has been asked previously but with regards to improving performance: Using Cython with Django. Does it make sense?之前已经问过一个类似的问题,但关于提高性能: Using Cython with Django.Does it mean sense?

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. 我们制造商业产品,安装在客户端,以管理他们的Genesys电力联络中心。 The core of the application is written in Django and we wanted to protect (limit) the code from inspection. 该应用程序的核心是用Django编写的,我们希望保护(限制)代码免受检查。

There is a speed improvement from running in native python but it is not a considerable difference. 在本机python中运行可以提高速度,但这并没有太大的差别。 The improvement depends on the type of task, sometimes up to 30% sometimes minimal. 改进取决于任务的类型,有时最高可达30%。

We run into issues from time to time where something works in Python but then it does not in Cython. 我们不时遇到问题,其中某些东西在Python中有效,但在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 目前版本使用Django 1.11在Python 3.5上运行

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.我在项目主目录中创建了一个 setup.py 文件。

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.扫描您的应用程序目录并将文件添加到 fileSet 中,无论您想编译什么。 file1.py, file2.py and file3.py are just examples only. file1.py、file2.py 和 file3.py 只是示例。

Finally, just run the setup.py file as below最后,运行 setup.py 文件如下

python setup.py build_ext --inplace 

Then Cython stats compiling each file and makes it .so file.然后 Cython stats 编译每个文件并使其成为 .so 文件。 Example: app1/file1.so app2/file2.so app3/file3.so示例: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.删除所有 .py 和 .pyc 文件。 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.我试过NGINX,uWSGI。

Good Luck.祝你好运。

Please checkout djcompiler package that compiles django projects using Cython.请检查使用 Cython 编译 django 项目的 djcompiler package。 https://github.com/abdoohossamm/djcompiler https://github.com/abdoohossamm/djcompiler

run pip install djcompiler运行pip install djcompiler

then head to project directory and run djcompiler buildfile Edit your configurations in.djcompiler file.然后前往项目目录并运行djcompiler buildfile在 .djcompiler 文件中编辑您的配置。 run djcompiler compile运行djcompiler compile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM