Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CherryPy压井工艺(如果未及时ping)_Javascript_Process_Kill_Cherrypy - Fatal编程技术网

Javascript CherryPy压井工艺(如果未及时ping)

Javascript CherryPy压井工艺(如果未及时ping),javascript,process,kill,cherrypy,Javascript,Process,Kill,Cherrypy,有没有办法让CherryPy(运行在:8080上,它是SIGUSR1的唯一侦听器)在进程在一定的秒数内没有ping的情况下杀死它 当然,用于进程终止的Python代码没有问题,只是CherryPy检测最后一次ping并不断将其与当前时间进行比较的方式——如果进程在一定的秒数内没有被ping,则终止进程 请注意,如果Javascript正在执行ping(通过setInterval()),CherryPy代码中的无限循环将导致.ajax()请求挂起和/或超时,除非有办法让.ajax()只执行ping

有没有办法让CherryPy(运行在:8080上,它是SIGUSR1的唯一侦听器)在进程在一定的秒数内没有ping的情况下杀死它

当然,用于进程终止的Python代码没有问题,只是CherryPy检测最后一次ping并不断将其与当前时间进行比较的方式——如果进程在一定的秒数内没有被ping,则终止进程

请注意,如果Javascript正在执行ping(通过
setInterval()
),CherryPy代码中的无限循环将导致
.ajax()
请求挂起和/或超时,除非有办法让
.ajax()
只执行ping而不等待任何类型的响应

谢谢你们提供的任何建议


Mason

好的,所以答案是设置两个类,一个更新时间,另一个持续检查时间戳是否在20秒内没有更新。如果整个站点不是建立在CherryPy上的,那么当用户离开页面时,这非常有用。在我的例子中,它只是坐在:8080上听来自Zend项目的JS ping。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())
import cherrypy
导入操作系统
导入时间
类(对象):
@樱桃树
def索引(自):
全局变量
var=time.time()
@樱桃树
def其他(自我):
while(time.time()-var 20s old
setInterval(ping,15000);//每15秒更新一次时间变量,这样当用户在页面上时,观察者将永远不会终止进程
希望这能帮助其他人寻找一个类似的解决方案,一旦用户离开一个页面,就可以进行杀戮

石匠

<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>