Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.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
Python 如何让长时间的任务;“可取消”;通过Tornado HTTP服务器上的HTTP?_Python_Http_Asynchronous_Tornado_Request Cancelling - Fatal编程技术网

Python 如何让长时间的任务;“可取消”;通过Tornado HTTP服务器上的HTTP?

Python 如何让长时间的任务;“可取消”;通过Tornado HTTP服务器上的HTTP?,python,http,asynchronous,tornado,request-cancelling,Python,Http,Asynchronous,Tornado,Request Cancelling,我已经实现了某种繁重任务的HTTP包装器,我选择Tornado作为前端服务器框架(因为繁重的任务是用Python编写的,我只是习惯于Tornado) 目前,我只是直接从Tornado的过程中调用了繁重的任务。我使用jQuery编写了某种基于Web的界面,让它使用表单中设置的参数处理AJAX请求 正如您所想象的,我从web浏览器中抛出的任务是不可取消的。我可以取消的唯一方法是向Python进程发送9或15信号,而用户通常不能这样做 我想通过HTTP请求某种类型的“取消”请求来取消当前正在工作的任务

我已经实现了某种繁重任务的HTTP包装器,我选择Tornado作为前端服务器框架(因为繁重的任务是用Python编写的,我只是习惯于Tornado)

目前,我只是直接从Tornado的过程中调用了繁重的任务。我使用jQuery编写了某种基于Web的界面,让它使用表单中设置的参数处理AJAX请求

正如您所想象的,我从web浏览器中抛出的任务是不可取消的。我可以取消的唯一方法是向Python进程发送9或15信号,而用户通常不能这样做


我想通过HTTP请求某种类型的“取消”请求来取消当前正在工作的任务。怎样才能做到呢?大多数处理繁重任务的web服务(例如YouTube上的视频编码)都在做什么?

实际上Tornado的
期货
不支持取消()。此外,即使使用,时间路由作业仍在运行,只需等待结果

唯一的方法,如中所述,是以可以取消的方式实现逻辑(使用一些标志或任何东西)

例如:

  • 作业是一个简单的异步睡眠
  • /
    列出作业
  • /add/TIME
    添加新作业-时间(秒)-指定睡眠时间
  • /cancel/ID
    取消作业
代码可能如下所示:

from tornado.ioloop import IOLoop
from tornado import gen, web
from time import time

class Job():

    def __init__(self, run_sec):
        self.run_sec = int(run_sec)
        self.start_time = None
        self.end_time = None
        self._cancelled = False

    @gen.coroutine
    def run(self):
        """ Some job

        The job is simple: sleep for a given number of seconds.
        It could be implemented as:
             yield gen.sleep(self.run_sec)
        but this way makes it not cancellable, so
        it is divided: run 1s sleep, run_sec times 
        """
        self.start_time = time()
        deadline = self.start_time + self.run_sec
        while not self._cancelled:
            yield gen.sleep(1)
            if time() >= deadline:
                break
        self.end_time = time()

    def cancel(self):
    """ Cancels job

    Returns None on success,
    raises Exception on error:
      if job is already cancelled or done
    """
        if self._cancelled:
            raise Exception('Job is already cancelled')
        if self.end_time is not None:
            raise Exception('Job is already done')
        self._cancelled = True

    def get_state(self):
        if self._cancelled:
            if self.end_time is None:
                # job might be running still
                # and will be stopped on the next while check
                return 'CANCELING...'
            else:
                return 'CANCELLED'
        elif self.end_time is None:
            return 'RUNNING...'
        elif self.start_time is None:
            # actually this never will shown
            # as after creation, job is immediately started
            return 'NOT STARTED'
        else:
            return 'DONE'


class MainHandler(web.RequestHandler):

    def get(self, op=None, param=None):
        if op == 'add':
            # add new job
            new_job = Job(run_sec=param)
            self.application.jobs.append(new_job)
            new_job.run()
            self.write('Job added')
        elif op == 'cancel':
            # cancel job - stop running
            self.application.jobs[int(param)].cancel()
            self.write('Job cancelled')
        else:
            # list jobs
            self.write('<pre>') # this is so ugly... ;P
            self.write('ID\tRUNSEC\tSTART_TIME\tSTATE\tEND_TIME\n')
            for idx, job in enumerate(self.application.jobs):
                self.write('%s\t%s\t%s\t%s\t%s\n' % (
                    idx, job.run_sec, job.start_time,
                    job.get_state(), job.end_time
                ))


class MyApplication(web.Application):

    def __init__(self):

        # to store tasks
        self.jobs = []

        super(MyApplication, self).__init__([
            (r"/", MainHandler),
            (r"/(add)/(\d*)", MainHandler),
            (r"/(cancel)/(\d*)", MainHandler),
        ])

if __name__ == "__main__":
    MyApplication().listen(8888)
    IOLoop.current().start()
然后取消一些。。。注意-它只需要龙卷风

这个例子很简单,
gen.sleep
是你的繁重任务。当然,并不是所有的工作都像以可取消的方式实现的那样简单

for a in `seq 12 120`; do curl http://127.0.0.1:8888/add/$a; done