0

什么是强制 python 从 NSTask 以 64 模式运行的方法?

更新:根据 Ned 的建议,我尝试使用目标 c 直接引用 Python2.7 并且有效。将 @"/usr/bin/python" 更改为 @"/usr/bin/python2.7"。新代码位于问题的底部。

我有一个 64 位系统。从终端运行时,python 以 64 位运行。当我从运行 /usr/bin/uname -m 的 NSTask 运行普通 shell 时,它返回 x86_64。

我尝试过使用 arch,但运行 python 的 shell 仍处于 32 位模式。

示例方法

-(void) runPython64BitScriptViaArchWithPath:(NSString*)path {

    NSTask* task = [[NSTask alloc] init];
    task.launchPath = @"/usr/bin/arch" ;
    task.arguments = [NSArray arrayWithObjects: @"-x86_64", @"/usr/bin/python", path, nil];

    [task setStandardInput:[NSPipe pipe]] ;

    NSPipe *stdOutPipe = nil;
    stdOutPipe = [NSPipe pipe];
    [task setStandardOutput:stdOutPipe];

    NSPipe* stdErrPipe = nil;
    stdErrPipe = [NSPipe pipe];
    [task setStandardError: stdErrPipe];

    NSLog(@"%@", [task arguments]) ;

    [task launch] ;

    NSData* data = [[stdOutPipe fileHandleForReading] readDataToEndOfFile];

    [task waitUntilExit];

    NSInteger exitCode = task.terminationStatus;

    if (exitCode != 0)
    {
        NSLog(@"Error!");
        NSData *error = [[stdErrPipe fileHandleForReading] readDataToEndOfFile] ;
        NSLog(@"Exit code : %ld", (long)exitCode) ;
        NSString *result = [[NSString alloc] initWithBytes: error.bytes length:error.length encoding: NSUTF8StringEncoding] ;
        NSLog(@"%@",result);
        [result release];
    } else {
        NSString *result = [[NSString alloc] initWithBytes: data.bytes length:data.length encoding: NSUTF8StringEncoding] ;
        NSLog(@"%@",result) ;
        [result release];
    }
    [task release] ;

}

如果从终端运行,则可以工作的示例 python 脚本

#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'social_shields.settings'

try:
    import hosts.models
    shield = hosts.models.Shield.objects.all()[0]
    shield.active = True
    shield.save()
except Exception as exception:
    import struct
    print 'Bits : %s' % ( 8 * struct.calcsize("P"))
    print exception

示例日志

2013-07-04 16:10:31.600 socialshield[88688:303] onShieldDown
2013-07-04 16:10:31.607 socialshield[88688:303] x86_64
2013-07-04 16:10:31.607 socialshield[88688:303] (
    "-x86_64",
    "/usr/bin/python",
    "/source/social_shields/social_shields/shield_down.py"
)
2013-07-04 16:10:31.933 socialshield[88688:303] Bits : 32
Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): no suitable image found.  Did find:
    /Library/Python/2.7/site-packages/_mysql.so: mach-o, but wrong architecture

应用 Ned 的建议后有效的新代码 :-)

-(void) runPython64BitScriptViaArchWithPath:(NSString*)path {

    NSTask* task = [[NSTask alloc] init];
    task.launchPath = @"/usr/bin/arch" ;
    task.arguments = [NSArray arrayWithObjects: @"-x86_64", @"/usr/bin/python2.7", path, nil];

    [task setStandardInput:[NSPipe pipe]] ;

    NSPipe *stdOutPipe = nil;
    stdOutPipe = [NSPipe pipe];
    [task setStandardOutput:stdOutPipe];

    NSPipe* stdErrPipe = nil;
    stdErrPipe = [NSPipe pipe];
    [task setStandardError: stdErrPipe];

    NSLog(@"%@", [task arguments]) ;

    [task launch] ;

    NSData* data = [[stdOutPipe fileHandleForReading] readDataToEndOfFile];

    [task waitUntilExit];

    NSInteger exitCode = task.terminationStatus;

    if (exitCode != 0)
    {
        NSLog(@"Error!");
        NSData *error = [[stdErrPipe fileHandleForReading] readDataToEndOfFile] ;
        NSLog(@"Exit code : %ld", (long)exitCode) ;
        NSString *result = [[NSString alloc] initWithBytes: error.bytes length:error.length encoding: NSUTF8StringEncoding] ;
        NSLog(@"%@",result);
        [result release];
    } else {
        NSString *result = [[NSString alloc] initWithBytes: data.bytes length:data.length encoding: NSUTF8StringEncoding] ;
        NSLog(@"%@",result) ;
        [result release];
    }
    [task release] ;

}
4

1 回答 1

1

我假设您已经验证了它/Library/Python/2.7/site-packages/_mysql.so是 64 位的,并且在这两种情况下您确实使用了相同的 Python。在 OS X 10.6 及更高版本的系统上,/usr/bin/python实际上是一个包装器可执行文件,用于确定要运行的 Python 版本和架构(32 位或 64 位);详情见man 1 python。尝试/usr/bin/python2.7直接执行。这应该默认为 64 位。您还可以使用以下记录的测试检查 Python 是在 32 位还是 64 位模式下运行:

import sys; print(sys.maxsize > 2**32)
于 2013-07-04T06:14:10.747 回答