Re: Understanding the Run loop idea and the updating of controls during long operations
Re: Understanding the Run loop idea and the updating of controls during long operations
- Subject: Re: Understanding the Run loop idea and the updating of controls during long operations
- From: Andreas Grosam <email@hidden>
- Date: Mon, 8 Nov 2010 18:30:01 +0100
On Nov 7, 2010, at 4:35 PM, eveningnick eveningnick wrote:
>> - Multiple threads, figure out your bug with synchronization around
>> AppleScripts (there is no reason the library would run slower just because
>> you add a new thread).
>>
> I am not doing no synchronization at all, nor i need any for my application.
> I was thinking this is done by the framework, only because i was reading
> that NSAppleScript wasn't thread safe, and became so only lately. But as
> long as "we shouldn't care how is it built inside", i only assumed this
> thread safety had been provided in exchange of performance (when the apple
> events - this is how NSAppleScript allows the application to retrieve result
> value from applescript - are still received in the main thread, which
> suspends both threads or something). Well actially i get a general
> performance slowdown.
On >10.6 you can safely execute Apple scripts from a secondary thread (see previous thread on this matter).
Well, I just checked this using an NSOperation object which executes a script. The operation can be alternatively executed on the main thread scheduled by the global queue [NSOperation mainQueue] or it can be executed on a secondary thread scheduled by any queue which you get via [[NSOperationQueue alloc] init]. There is no noticeable difference in the execution time of the script, which takes about 15 seconds. (As a contrived example, the script launches TextEdit which itself opens a text file of about several kByte. The script counts the words, builds a list of words and an internal sort routine sorts these words and returns the sorted list.)
The Cocoa app is just a few lines - and it doesn't require any thoughts on how to handle the run loop -- there is simply no need for this. There is no need for synchronization as well, unless you schedule a number of operations concurrently which modify the same object (say a file, or whatever).
I should note however, that a script is not "cancelable" -- it performs "atomically" regarding the NSOperation's main method. So, in order to make the NSOperation's main method interruptible, you need to partition your work in a sequence of smaller actions executed by one or more scripts. Then call them in sequence -- preferable in a loop. Before you execute the next script you check the cancellation state of the NSOperation. Just return from -main when someone had cancelled the operation. The operation can send messages about the progress to its delegate (note: custom NSOperation, which defines also an appropriate delegate protocol) in every loop as well, and may return a result when it eventually finished.
When the NSOperation executes on a secondary thread, the app's main thread isn't blocked and receives and processes input without any noticeable delay.
The most challenging task is probably to partition a given script, so that the task becomes interruptible when executed in a NSOperation.
Regards,
Andreas_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden