[Pythonmac-SIG] newbie: threads in PyObjC

Nehemiah Dacres vivacarlie at gmail.com
Wed Jun 27 18:51:24 CEST 2007


is this a question or an answer? is this a cocoa question or a PyObjC
question?

On 6/6/07, Tom Elliott <telliott99 at mac.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.  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.
>
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG at python.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&mode=thread&cid=15927703
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythonmac-sig/attachments/20070627/28f3d7a0/attachment.html 


More information about the Pythonmac-SIG mailing list