如何将多个Python源文件连接成单个文件? [英] How to concatenate multiple Python source files into a single file?

查看:431
本文介绍了如何将多个Python源文件连接成单个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(假设:应用程序的启动时间非常关键;我的应用程序启动很多;我的应用程序运行在导入速度比平常慢的环境中;需要导入许多文件;编译为 .pyc files is not available。)



我想连接所有Python源文件它将模块集合定义为一个新的Python源文件。



我想导入新文件的结果就好像我导入了一个原始文件(然后会导入更多原始文件,等等)。



这可能吗?



这里粗略的手动模拟了一个工具在提供模块bar和baz的源文件时可能产生的结果。您可以在部署代码之前运行这样的工具。

  __ file__ ='foo.py'

def _module(_name):
导入类型
mod = types.ModuleType(name)
mod .__ file__ = __file__
sys.modules [module_name] = mod
返回mod

def _bar_module():

def hello():
print'Hello World! BAR'

mod = create_module('foo.bar')
mod.hello = hello
return mod

bar = _bar_module()
del_bar_module
$ b $ def _baz_module():

def hello():
print'Hello World! BAZ'

mod = create_module('foo.bar.baz')
mod.hello = hello
返回mod

baz = _baz_module()
del _baz_module

现在您可以:

  from foo.bar import hello 
hello()

这段代码没有考虑导入语句和依赖关系等内容。是否有任何现有的代码会使用这个或其他技术来组装源文件?

这与在发送之前用于组装和优化JavaScript文件的工具非常相似到浏览器,多个HTTP请求的延迟会影响性能。在这种Python情况下,它是在启动时导入数百个Python源文件的延迟。这种情况下会受到伤害。

解决方案

app引擎作为标签显示,请确保您使用这个成语

  def main():
#do stuff
if __name__ =='__main__':
main()

由于GAE每次请求都不会重新启动您的应用程序,除非.py已经更改,它只是再次运行 main()

这个技巧可以让你编写CGI风格的应用程序,而无需启动性能。



缓存脚本。否则,为每个
请求加载
处理程序脚本。



(Assume that: application start-up time is absolutely critical; my application is started a lot; my application runs in an environment in which importing is slower than usual; many files need to be imported; and compilation to .pyc files is not available.)

I would like to concatenate all the Python source files that define a collection of modules into a single new Python source file.

I would like the result of importing the new file to be as if I imported one of the original files (which would then import some more of the original files, and so on).

Is this possible?

Here is a rough, manual simulation of what a tool might produce when fed the source files for modules 'bar' and 'baz'. You would run such a tool prior to deploying the code.

__file__ = 'foo.py'

def _module(_name):
    import types
    mod = types.ModuleType(name)
    mod.__file__ = __file__
    sys.modules[module_name] = mod
    return mod

def _bar_module():

    def hello():
        print 'Hello World! BAR'

    mod = create_module('foo.bar')
    mod.hello = hello
    return mod

bar = _bar_module()
del _bar_module

def _baz_module():

    def hello():
        print 'Hello World! BAZ'

    mod = create_module('foo.bar.baz')
    mod.hello = hello
    return mod

baz = _baz_module()
del _baz_module

And now you can:

from foo.bar import hello
hello()

This code doesn't take account of things like import statements and dependencies. Is there any existing code that will assemble source files using this, or some other technique?

This is very similar idea to tools being used to assemble and optimise JavaScript files before sending to the browser, where the latency of multiple HTTP requests hurts performance. In this Python case, it's the latency of importing hundreds of Python source files at startup which hurts.

解决方案

If this is on google app engine as the tags indicate, make sure you are using this idiom

def main(): 
    #do stuff
if __name__ == '__main__':
    main()

Because GAE doesn't restart your app every request unless the .py has changed, it just runs main() again.

This trick lets you write CGI style apps without the startup performance hit

AppCaching

If a handler script provides a main() routine, the runtime environment also caches the script. Otherwise, the handler script is loaded for every request.

这篇关于如何将多个Python源文件连接成单个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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