List Info

Thread: newbie: threads in PyObjC




newbie: threads in PyObjC
country flaguser name
United States
2007-06-06 19:37:01
The kill() method is just a stub, written back when I thought the way to do this is for the main thread to terminate the worker thread.  Now, after more reading, I realize it's better to have the worker test a condition periodically and exit (gracefully) if necessary.

The withObject_ parameter can be used to pass a reference back to the original thread.  This is useful when the new thread executes a method in a different class.  I ended up just calling a method in the same class, like this:

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_'
        self.continueFlag = True
        #t = Threaded.alloc().init()
        NSThread.detachNewThreadSelector_toTarget_withObject_(
            'newThread:', self, None)

        

    def kill_(self, sender):
        print 'kill_'
        self.continueFlag = False

            

    objc.signature('v:')
    def newThread_(sender):
        pool = NSAutoreleasePool.alloc().init()
        print 'Threaded', sender
        for i in range(10):
            if sender.continueFlag:
                time.sleep(1)
                print i
        del pool

Thanks.
Re: newbie: threads in PyObjC
user name
2007-06-27 11:51:24
is this a question or an answer? is this a cocoa question or a PyObjC question?

On 6/6/07, Tom Elliott < telliott99mac.com">telliott99mac.com > wrote:
The kill() method is just a stub, written back when I thought the way to do this is for the main thread to terminate the worker thread.&nbsp; Now, after more reading, I realize it's better to have the worker test a condition periodically and exit (gracefully) if necessary.

The withObject_ parameter can be used to pass a reference back to the original thread.&nbsp; This is useful when the new thread executes a method in a different class.&nbsp; I ended up just calling a method in the same class, like this:

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

 &nbsp;  

    def go_(self, sender):
        print 'go_&#39;
        self.continueFlag = True
        #t = Threaded.alloc().init()
        NSThread.detachNewThreadSelector_toTarget_withObject_(
            'newThread:', self, None)

 &nbsp;      

    def kill_(self, sender):
        print 'kill_'
        self.continueFlag = False

 &nbsp;          

    objc.signature('v:' )
    def newThread_(sender):
        pool = NSAutoreleasePool.alloc().init()
        print 'Threaded', sender
        for i in range(10 ):
            if sender.continueFlag:
                time.sleep(1 )
                print i
        del pool

Thanks.

_______________________________________________
Pythonmac-SIG maillist  -  python.org"> Pythonmac-SIGpython.org
http://mail.python.org/mailman/listinfo/pythonmac-sig




--

"lalalalala! it's not broken because I can use it"

http://linux.slashdot.org/comments.pl?sid=194281&;threshold=1&commentsort=0&amp;mode=thread&cid=15927703
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )