0

NSTask から python を強制的に 64 モードで実行する方法は何ですか?

更新: Ned の提案に従って、目的の c で Python2.7 を直接参照しようとしたところ、うまくいきました。@"/usr/bin/python" を @"/usr/bin/python2.7" に変更しました。新しいコードは質問の一番下にあります。

私は64ビットシステムを持っています。ターミナルから実行すると、python は 64 ビットで実行されます。/usr/bin/uname -m を実行している NSTask からプレーン シェルを実行すると、x86_64 が返されます。

Arch を使用してみましたが、Python を実行しているシェルはまだ 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

ネッドの提案を適用した後に機能する新しいコード:-)

-(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 に答える