部署时应该生成* .pyc文件吗? [英] Should I generate *.pyc files when deploying?

查看:137
本文介绍了部署时应该生成* .pyc文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发Python网络应用程序(Flask / uWSGI)并在我的本地计算机上运行它时,解释器会生成 *。pyc 文件。我的理解是,这些编译后的文件可使加载更快,但不一定 run 更快。

When developing a Python web app (Flask/uWSGI) and running it on my local machine, *.pyc files are generated by the interpreter. My understanding is that these compiled files can make things load faster, but not necessarily run faster.

当我将该同一个应用程序部署到生产环境中,它以在本地文件系统上没有写权限的用户帐户运行。没有 *。pyc 文件提交到源代码管理,并且在部署过程中不做任何努力来生成它们。即使Python想要在运行时编写 .pyc 文件,它也无法执行。

When I deploy this same app to production, it runs under a user account that has no write permissions on the local file system. There are no *.pyc files committed to source control, and no effort is made to generate them during the deploy. Even if Python wanted to write a .pyc file at runtime, it would not be able to.

最近我开始想知道这是否会对应用程序的性能产生明显的影响,无论是在流程启动后的第一次浏览量还是在整个生命周期中都是一致的。

Recently I started wondering if this has any tangible effect on the performance of the app, either in terms of the very first pageview after the process starts, or consistently throughout its entire lifetime.

我应该在部署脚本中放入 python -m compileall 吗?

Should I throw a python -m compileall in as part of my deploy scripts?

推荐答案

当然,您可以继续进行预编译为 .pyc ,因为它不会造成任何伤害。

Sure, you can go ahead and precompile to .pyc's as it won't hurt anything.

这会影响第一个或第n个页面加载吗?假设Flask / WSGI是一个持久性进程,而不是一个持久性进程。到请求第一页时,所有Python模块都将已加载到内存中(作为字节码)。因此,服务器启动时间将是唯一不受未预编译文件影响的事情。

Will it affect the first or nth pageload? Assuming Flask/WSGI runs as a persistent process, not at all. By the time the first page has been requested, all of the Python modules will have already been loaded into memory (as bytecode). Thus, server startup time will be the only thing affected by not having the files pre-compiled.

但是,如果由于某种原因为每个页面调用了一个新的Python进程请求,然后是的,在性能上(可能)会有明显的差异,最好进行预编译。

However, if for some reason a new Python process is invoked for each page request, then yes, there would (probably) be a noticeable difference in performance and it would be better to pre-compile.

正如克劳斯在上述评论中所说,只有在其他情况下,页面加载可能会受到影响,如果函数碰巧尝试导入尚未导入的模块。这将要求模块被解析并转换为字节码,然后再加载到内存中,然后才能继续。

As Klaus said in the comments above, the only other time a pageload might be affected is if a function happens to try and import a module that hasn't already been imported. This will require the module to be parsed and converted to bytecode then loaded into memory before being able to continue.

这篇关于部署时应该生成* .pyc文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆