2013-01-17 51 views
1

我有一個AppleScript腳本,它運行遞歸函數來計算插入插件的實例。 每插入一個插件後,該功能將檢查CPU並確定是否有CPU超載。Applescript - 遞歸函數中的堆棧溢出

如果發現CPU超載,它會開始刪除插件,直到達到令人滿意的程度。 然後它返回在計算機上加載的實例的數量。

問題是我在運行一定數量後出現堆棧溢出。 AppleScript是否具有內部遞歸線程的限制?

on plugin_recurse(mode, plugin_name, component, track_count, instance_count, has_ref, min_instances, last_max) 
    try 
     log "mode - " & mode 
     if mode is "ret" then return {track_count, instance_count, last_max} 

     if mode is "add" then 
      if (instance_count - (10 * track_count) = 0) then 
       create_track(component) 
       set track_count to track_count + 1 
      end if 
      set instance_count to instance_count + 1 
      insert_plugin(plugin_name, component, track_count, instance_count) 
      if has_ref then 
       set CPUover to false 
       if min_instances = 1 then 
        set mode to "ret" 
       else 
        set min_instances to min_instances - 1 
       end if 
      else 
       set {CPUover, last_max} to check_cpu(last_max) 
      end if 
      if CPUover then 
       set mode to "sub" 
      end if 
     end if 

     if mode is "sub" then 
      if instance_count > 1 then 
       remove_plugin(plugin_name, component, track_count, instance_count) 
       set instance_count to instance_count - 1 
       if ((10 * track_count) - instance_count = 10) then 
        remove_track(track_count) 
        set track_count to track_count - 1 
       end if 
       set {CPUover, last_max} to check_cpu(last_max) 
       if not CPUover then 
        set mode to "ret" 
       end if 
      else 
       set mode to "ret" 
      end if 
     end if 
     plugin_recurse(mode, plugin_name, component, track_count, instance_count, has_ref, min_instances, last_max) 
    on error err 
     error err 
    end try 
end plugin_recurse 
+0

請將代碼完整發布。 – adayzdone

+0

我發佈了遞歸函數的完整代碼。 – Nir

回答

0

的問題是,我得到一個堆棧溢出一定量的 運行後。 AppleScript是否具有內部遞歸線程的限制?

是的,AppleScript僅限於許多邊界。您的錯誤是由處理程序(函數)調用的深度限制引起的。在我的機器上,級別限制設置爲577.這種限制對於OOP語言來說非常普遍,因爲運行代碼的'虛擬機'需要它的限制。例如,Objective-C在遞歸中也存在限制。如果你需要更多,你的代碼被認爲是不好的代碼,你應該嘗試使用正常的循環。但是,我不得不承認,與其他OOP的限制相比,577並不是一個很高的數字。

對於這樣的代碼,在不確定會有多少遞歸的情況下,使用循環通常比遞歸更好。

+0

謝謝。我結束了使用正常的循環,並解決了這個問題。 – Nir