さて、答えは2つのクラスを設定することでした。1つは時刻を更新し、もう1つはタイムスタンプが20秒以内に更新されていないかどうかを常にチェックします。これは、サイト全体がCherryPyで構築されていない場合に、ユーザーがページを離れたときにプロセスを強制終了する場合に非常に役立ちます。私の場合、それは単に:8080に座って、ZendプロジェクトからのJSpingをリッスンしています。CherryPyコードは次のようになります。
import cherrypy
import os
import time
class ProcKiller(object):
@cherrypy.expose
def index(self):
global var
var = time.time()
@cherrypy.expose
def other(self):
while(time.time()-var <= 20):
time.sleep(1)
print var
os.system('pkill proc')
cherrypy.quickstart(ProcKiller())
pingを実行するJSは、文字通り次のように単純です。
<script type="text/javascript">
function ping(){
$.ajax({
url: 'http://localhost:8080'
});
}
function initWatcher(){
$.ajax({
url: 'http://localhost:8080/other'
});
}
ping(); //Set time variable first
initWatcher(); //Starts the watcher that waits until the time var is >20s old
setInterval(ping, 15000); //Updates the time variable every 15s, so that while users are on the page, the watcher will never kill the process
</script>
これが、ユーザーがページを離れたときに殺害を処理するための同様の解決策を探している他の誰かに役立つことを願っています!
石工