I'm using the PyObjC bridge. I have a computation that will be triggered by a user. It takes about 2 minutes depending on the database we're using. I'd like the user to be able to kill it if he decides it's taking too long.

It seems like threading should be the solution. My simple demo based on what I read in NSThread related docs is as follows. (I've put the NSAutoreleasePool call in twice here, but tried it in either place, separately).

from Foundation import *
from AppKit import *
import objc, time

from PyObjCTools import NibClassBuilder

class PyThreadAppDelegate(NibClassBuilder.AutoBaseClass):
    def init(self):
        self = super(PyThreadAppDelegate, self).init()
        return self

    def go_(self, sender):
        print 'go_'
        pool = NSAutoreleasePool.alloc().init()
        NSThread.detachNewThreadSelector_toTarget_withObject_(
            'newThread:', Threaded, None)
        del pool

    def kill_(self, sender):
        pass

class Threaded(NSObject):

    @objc.signature('v:@')
    def newThread_(obj):
        print 'Threaded'
        pool = NSAutoreleasePool.alloc().init()
        for i in range(5):
            time.sleep(1)
            print i
        del pool

This dies with:

===== Monday, June 4, 2007 8:53:09 PM US/Eastern =====
go_
2007-06-04 20:53:11.414 PyThread[555] *** _NSAutoreleaseNoPool(): Object 0x1419cc0 of class NSCFString autoreleased with no pool in place - just leaking 2007-06-04 20:53:11.415 PyThread[555] *** +[Threaded newThread:]: selector not recognized 2007-06-04 20:53:11.415 PyThread[555] *** _NSAutoreleaseNoPool(): Object 0x146cb10 of class NSCFString autoreleased with no pool in place - just leaking 2007-06-04 20:53:11.415 PyThread[555] *** _NSAutoreleaseNoPool(): Object 0x146c9a0 of class NSCFString autoreleased with no pool in place - just leaking 2007-06-04 20:53:11.415 PyThread[555] *** _NSAutoreleaseNoPool(): Object 0x146c980 of class NSException autoreleased with no pool in place - just leaking
2007-06-04 20:53:11.415 PyThread[555] An uncaught exception was raised
2007-06-04 20:53:11.415 PyThread[555] *** +[Threaded newThread:]: selector not recognized 2007-06-04 20:53:11.415 PyThread[555] *** Uncaught exception: <NSInvalidArgumentException> *** +[Threaded newThread:]: selector not recognized
Jun  4 20:53:12 joan-olsons-computer-2 crashdump[556]: PyThread crashed
Jun 4 20:53:12 joan-olsons-computer-2 crashdump[556]: crash report written to: /Users/jolson/Library/Logs/CrashReporter/PyThread.crash.log

In addition, I don't know how to talk to the thread to kill it.

Thanks for any help,

Tom Elliott
_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig

Reply via email to