objective c - Cocoa app cannot start looping python script. Works when built/run through xcode -
i'm trying use nstask start looping python script cocoa app. python script ends in app's contents/resources folder.
i path script , create nstask with:
dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ nsstring *scriptpath = [[nsbundle mainbundle] pathforresource:filename oftype:@"py"]; nstask* task = [[nstask alloc] init]; task.launchpath = python_path; task.arguments = [nsarray arraywithobjects:scriptpath, nil]; [task launch]; [task waituntilexit]; });
(i've omitted stdoutpipe stuff readability)
when build & run app xcode works expected, task holds @ waituntilexit
, python script runs continuously.
when try run produced .app file gets waituntilexit
, stalls awhile, , exits error. have logging in python script know no part of script run during time. script path true location of python scripts , returns same path when run xcode or .app.
i figure must have of project settings set-up incorrectly, seems handling resources correctly. idea?
update 1 running non-looping python script print statement works .app. followings non-looping script fails well:
#!/usr/bin/python import os def log(string): print(string) path = os.environ['pwd'] + '/smoke_screen_test.txt' f1=open(path, 'a') f1.write(string + "\n") if __name__ == '__main__': log("test works") exit(0)
maybe import statements mess up? or file creation?
the problem use of os.environ['pwd']
. pwd not return local directory of script being executed. duh. solution grab path of script argv[0] , use directory of path:
path = os.path.dirname(sys.argv[0])
then pass path around needed.
Comments
Post a Comment