1

挿入されたプラグインのインスタンスをカウントする再帰関数を実行するAppleScriptスクリプトがあります。挿入された各プラグインの後で、関数はCPUをチェックし、CPUの過負荷があるかどうかを判断します。

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
4

1 に答える 1

0

問題は、一定量の実行後にスタックオーバーフローが発生することです。AppleScriptには内部再帰スレッドの制限がありますか?

はい、AppleScriptは多くの境界に制限されています。エラーは、ハンドラー(関数)呼び出しの深さの制限が原因で発生します。私のマシンでは、レベル制限は577に設定されています。コードを実行する「仮想マシン」には制限が必要なため、このような制限はOOP言語では非常に一般的です。たとえば、Objective-Cには再帰にも制限があります。さらに必要な場合は、コードが不適切なコーディングと見なされるため、通常のループを使用してみてください。ただし、他のOOPの制限と比較して577はそれほど多くないことを認めなければなりません。

このようなコードでは、再帰がいくつあるかわからない場合は、通常、再帰よりもループを使用する方が適切です。

于 2013-01-22T02:09:42.360 に答える