如果在删除后立即调用,则不会修改请求返回304

时间:2018-05-24 18:50:21

标签: python mongodb pymongo tornado

我正在尝试测试我的其余api,但是我发现了一个奇怪的行为,如果我在删除后立即创建一个get请求它将触发304并返回已删除的对象。我不太清楚为什么会这样。如果我等了一秒,它将返回一个200代码删除对象?

日志

2018-05-24 20:38:26,881 - tornado.access - INFO - 200 DELETE 
/api/v1/destinations/5b0706984db64f0b3ad92e13 (127.0.0.1) 0.73ms

2018-05-24 20:38:26,929 - tornado.access - INFO - 304 GET 
/api/v1/destinations (127.0.0.1) 39.74ms

休息api

import tornado.web
import tornado.gen



class Handler(base.BaseHandler):

    def _get_destination(self, destination_id):
        destination = self.datastore.get_destination(destination_id)
        if not destination:
            self.set_status(400)
            return {'error': 'Destination not found: %s' % destination_id}
        return destination


    def _get_destinations(self):
        destination = self.datastore.get_destinations()
        return destination

    @tornado.concurrent.run_on_executor
    def get_destinations(self):
        return self._get_destinations()

    @tornado.gen.engine
    def get_destinations_yield(self):
        """Wrapper for get_jobs in async mode."""
        return_json = yield self.get_destinations()
        self.finish(return_json)

    @tornado.web.removeslash
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        self.get_destinations_yield()


    def _delete_job(self, destination_id):
        destination = self._get_destination(destination_id)

        self.datastore.delete_destination(destination_id)


    @tornado.concurrent.run_on_executor
    def delete_job(self, destination_id):
        self._delete_job(destination_id)

    @tornado.gen.engine
    def delete_job_yield(self, destination_id):
        yield self.delete_job(destination_id)

    @tornado.web.removeslash
    @tornado.web.asynchronous
    @tornado.gen.engine
    def delete(self, destination_id):
        self.delete_job_yield(destination_id)

        response = {
            'destination': destination_id}
        self.set_status(200)
        self.finish(response)

pymongo功能

def delete_destination(self, destination_id):

    collection = self.get_collection(settings.DEST_COLLECTIONNAME)

    collection.remove({"_id": ObjectId(destination_id)})

def get_destinations(self):
    rows = self.get_collection(settings.DEST_COLLECTIONNAME).find({})

    return_json = {
        'destinations': [self._build_destination(row) for row in rows]}

    return return_json

1 个答案:

答案 0 :(得分:1)

你的_delete_job在Tornado返回200时被分离出来。当你试图在几毫秒后得到同样的东西时,_delete_job本身还没有完成