简体   繁体   English

为什么py2app .app比同一个python程序需要更长的时间才能启动?

[英]Why does a py2app .app take longer to launch than the same python program?

I'm not sure my question / title is phrased quite right. 我不确定我的问题/标题是否正确。 I'm working on OSX 10.6 and python 2.7.1. 我正在研究OSX 10.6和python 2.7.1。 I've used setuptools and py2app to create a .app, which I can run from the finder or from the terminal using open. 我已经使用setuptools和py2app创建了一个.app,我可以使用open从finder或终端运行。

One of my goals for the program is to launch quickly. 我对该计划的目标之一是快速启动。

When I go into the build folder and run the python program directly using python, my window pops up right away. 当我进入构建文件夹并使用python直接运行python程序时,我的窗口立即弹出。 In less than 1 second. 在不到1秒钟。 It's pretty consistent. 它非常一致。 But when I go into the dist folder and run the .app (either from the finder or with open), there is a several second pause before the window shows up. 但是当我进入dist文件夹并运行.app(从发现者或打开)时,在窗口出现之前有几秒钟的暂停。 About 4 to 5 seconds, pretty consistently. 大约4到5秒,非常一致。

I thought maybe it had something to do with open trying to find a document or something, so I tried this: 我想也许这与打开试图找到文件或其他东西有关,所以我尝试了这个:

open -a testrun.app ""

..and sure enough, the window pops right away! ..果然,窗户立即弹出!

Is there something I need to do in the setup.py or some place to tell it that this is not a document-oriented program? 我需要在setup.py或某个地方做些什么来告诉它这不是面向文档的程序吗?

A little more detail - 更多细节 -

I'm working on OSX 10.6.8 with Python 2.7.1 (as the system python). 我正在使用Python 2.7.1(作为系统python)使用OSX 10.6.8。 I've tried some minor variations on this (2.6 with python_select, 2.7.3 in a virtualenv..), but so far, it hasn't made any difference. 我已经尝试了一些微小的变化(2.6与python_select,2.7.3在virtualenv ..),但到目前为止,它没有任何区别。

I created a simple .dylib (in objective-c) which exports a function that opens a window using cocoa. 我创建了一个简单的.dylib(在objective-c中),它导出一个使用cocoa打开窗口的函数。 I created a very simple python extension module (in c) that has a function in it that calls the .dylib function. 我创建了一个非常简单的python扩展模块(在c中),其中有一个调用.dylib函数的函数。

(My plan is to create a platform-specific shared/dynamic library in a platform language for gui calls and related platform-specific calls, and a cross-platform c library that uses that, then create high level language extension modules that make the c library available to those languages.) (我的计划是用平台语言创建一个特定于平台的共享/动态库,用于gui调用和相关的特定于平台的调用,以及一个使用它的跨平台c库,然后创建高级语言扩展模块,使其成为c图书馆可以使用这些语言。)

I wrote a very simple python program that calls the c function. 我写了一个非常简单的python程序,它调用了c函数。 I wrote a setup.py that builds everything and uses py2app to create a .app. 我编写了一个setup.py来构建所有内容并使用py2app创建一个.app。

Here's the build script for the .dylib: 这是.dylib的构建脚本:

gcc -framework Cocoa -dynamiclib -x objective-c testlib.objc -current_version 1.0 -compatibility_version 1.0 -o libTestlib.1.dylib -arch i386 -arch x86_64

Here's the setup.py: 这是setup.py:

from setuptools import setup, Extension

APP = ['testrun.py']
DATA_FILES = []
OPTIONS = {
    'argv_emulation': True,
    'frameworks': ['/Users/shavais/scratch/objc/libTestlib.1.dylib']
}

module1 = Extension(
    'demo',
    sources = ['demo.c'],
    libraries = ['Testlib.1'],
    library_file_directories = ['/Users/shavais/scratch/objc']
)

setup(
    name = 'testrun',
    description = 'This is a testrun package',
    app = APP,
    data_files = DATA_FILES,
    options = {'py2app': OPTIONS},
    setup_requires = ['py2app'],
    version = '1.0',
    py_modules = ['testrun'],
    ext_modules = [module1]
)

I don't know about py2app on Mac (never used it) but if it works similar to http://www.pyinstaller.org/ , then the binary has to unpack the python runtime, your program and all the modules into memory. 我不知道Mac上的py2app(从未使用它),但如果它的工作方式类似于http://www.pyinstaller.org/ ,那么二进制文件必须将python运行时,程序和所有模块解压缩到内存中。 This takes time. 这需要时间。

Set "argv_emulation" to False unless you actually need that functionality (that is, unless you want to be able to drop files on your application bundle and then have those file names as arguments in sys.argv). 将“argv_emulation”设置为False,除非您确实需要该功能(即,除非您希望能够删除应用程序包上的文件,然后将这些文件名作为sys.argv中的参数)。

"Argv_emulation" is a hack for converting AppleEvents that request opening files into extra arguments in sys.argv. “Argv_emulation”是将请求打开文件的AppleEvents转换为sys.argv中的额外参数的黑客攻击。 To do this the emulation code spins up an event loop and waits for those open events or a timeout. 为此,仿真代码会旋转事件循环并等待这些打开事件或超时。 You are likely running into that timeout. 你可能会遇到那个超时。

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

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