从内存导入pyc? [英] importing pyc from memory?

查看:92
本文介绍了从内存导入pyc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python / C API,有没有办法可以导入一个内存中的pyc文件(而不是从磁盘加载)?


我正在尝试以专有文件格式保存已编译的python代码,以便在我的应用程序启动时减少编译所有脚本的开销

up 。


Derek

解决方案

Derek van Vliet启发我们:

我正在尝试以专有文件格式保存已编译的python代码,以减少在我的应用程序启动时编译所有脚本的开销。



为什么比在

文件系统上准备好.pyc文件更快?你为什么要用专有的文件格式呢?


Sybren

-

世界的问题是愚蠢。并不是说应该对愚蠢的死刑进行处罚,但为什么我们不要仅仅拿掉

安全标签来解决问题呢? br />
Frank Zappa




" Derek van Vliet" < DV ******* @ 3dna.net> schrieb im Newsbeitrag

新闻:11 ********************** @ g49g2000cwa.googlegr oups.com ...

使用Python / C API,有没有办法可以导入我在内存中的pyc文件(而不是从磁盘加载)?

我是'我试图以专有的文件格式保存已编译的python代码,以减少在我的应用程序启动时编译所有脚本的开销。



你可以使用zip文件作为专有文件格式?使用.pyc'或.py'中的
并将zippath添加到sys.path?


除了你可以加载代码.pyc的对象的内容在一个字符串中

喜欢


co = marshal.loads(pyc_s [8:])

mod = imp.new_module(''xy'')

exec co in mod .__ dict__

Robert


< blockquote>到目前为止,我已经在XML元素中定义了所有的python脚本,

,这是在我的程序使用Py_CompileString启动时编译的。

这有用太棒了,但我发现大部分时间我的应用程序

用来启动就花在那个Py_CompileString上,因为

大量的脚本我使用。


不幸的是,将这些脚本作为.pyc文件存储在磁盘上并不是我的选择。


Using the Python/C API, is there a way I can import a pyc file that I
have in memory (as opposed to loading from disk)?

I''m trying to save compiled python code in a proprietary file format to
cut reduce the overhead of compiling all my scripts when my app starts
up.

Derek

解决方案

Derek van Vliet enlightened us with:

I''m trying to save compiled python code in a proprietary file format
to cut reduce the overhead of compiling all my scripts when my app
starts up.



Why is that faster than having the .pyc files ready on your
filesystem? And why do you want it in a proprietary file format?

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don''t we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa



"Derek van Vliet" <dv*******@3dna.net> schrieb im Newsbeitrag
news:11**********************@g49g2000cwa.googlegr oups.com...

Using the Python/C API, is there a way I can import a pyc file that I
have in memory (as opposed to loading from disk)?

I''m trying to save compiled python code in a proprietary file format to
cut reduce the overhead of compiling all my scripts when my app starts
up.


you can use a zip file as "proprietary file format"? with .pyc''s or .py''s in
and add the zippath to sys.path ?

other than that you can load the code object of .pyc''s content in a string
like

co = marshal.loads( pyc_s[8:] )
mod=imp.new_module(''xy'')
exec co in mod.__dict__
Robert


Up to now, I''ve had all my python scripts defined in XML elements,
which were compiled when my program started up using Py_CompileString.
This has worked great, but I''m finding that most of the time my app
uses to start up is spent in that Py_CompileString, because of the
large number of scripts that I use.

Unfortunately, storing these scripts as .pyc files on disk is not an
option for me.


这篇关于从内存导入pyc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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