Django Tastypie缓慢的POST响应

Dar*_*ker 6 python api django tastypie

我想实现一个Tastypie资源,允许GET和下面的每用户权限策略POST操作,该模型是非常简单的(类似Tastypie文档中的注意事项型号)和资源本身也很简单,我只是有一个额外的override_urls方法,用Haystack实现搜索.

我现在的主要问题是,尽管在本地运行项目似乎工作正常,但请求速度快,而且一切都很快.一旦我部署了项目(On Linode,使用Nginx,Gunicorn,Runit),我发现POST请求太慢,大约需要1.1分钟才能返回201状态.另一方面,GET请求正如预期的那样运作良好.

我在请求上运行了一个Python Hotshot分析器,它显示整个POST请求占用0.127 CPU秒.我不太确定这里发生了什么.

我应该提一下,我正在为我的Tastypie资源使用ApiKeyAuthentication和DjangoAuthorization.

以下是Chrome Inspector针对请求的屏幕截图:http://d.pr/i/CvCS

如果有人能指引我找到正确的方向来寻找这个问题的答案,那将是很棒的.

谢谢!

编辑:

一些代码:

型号和资源:

class Note(models.Model):
    timestamp = models.DateTimeField('Timestamp')
    user = models.ForeignKey(User)
    page_title = models.CharField("Page Title", max_length=200)
    url = models.URLField('URL', verify_exists=False)
    summary = models.TextField("Summary")
    notes = models.TextField("Notes", null=True, blank=True)

    def __unicode__(self):
        return self.page_title

    def get_absolute_url(self):
        return self.url


class NoteResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user')

    class Meta:
        queryset = Note.objects.all()
        resource_name = 'note'
        list_allowed_methods = ['get', 'post']
        detail_allowed_methods = ['get']
        always_return_data = True
        authentication = ApiKeyAuthentication()
        authorization = DjangoAuthorization()
        # authentication = Authentication() #allows all access
        # authorization = Authorization() #allows all access

        ordering = [
            '-timestamp'
        ]

    def override_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/search%s$" % (
                self._meta.resource_name, trailing_slash()),
                self.wrap_view('get_search'), name="api_get_search"),
        ]

    def obj_create(self, bundle, request=None, **kwargs):
        return super(NoteResource, self).obj_create(bundle,
                                                        request,
                                                        user=request.user)

    def apply_authorization_limits(self, request, object_list):
        return object_list.filter(user=request.user)

    def get_search(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        self.is_authenticated(request)

        sqs = SearchQuerySet().models(Note).filter(
                                        user=request.user
                                    ).auto_query(
                                        request.GET.get('q', '')
                                    )

        paginator = Paginator(sqs, 100)

        try:
            page = paginator.page(int(request.GET.get('page', 1)))
        except InvalidPage:
            raise Http404("Sorry, no results on that page.")

        objects = []

        for result in page.object_list:
            bundle = self.build_bundle(obj=result.object, request=request)
            bundle.data['score'] = result.score
            bundle = self.full_dehydrate(bundle)
            objects.append(bundle)

        object_list = {
            'objects': objects,
        }

        self.log_throttled_access(request)
        return self.create_response(request, object_list)
Run Code Online (Sandbox Code Playgroud)

Gunicorn Conf:

bind = "0.0.0.0:1330"
workers = 1
Run Code Online (Sandbox Code Playgroud)

Nginx Conf(包含在主nginx.conf中):

server {
        listen 80;
        server_name domain.com example.com;
        access_log  /path/to/home/then/project/access.log;
        error_log /path/to/home/then/project/error.log;

        location / {
                proxy_pass   http://127.0.0.1:1330;
        }

        location /static/ {
                autoindex on;
                root /path/to/home/then/project/;
        }
}
Run Code Online (Sandbox Code Playgroud)

n3s*_*orm 3

OP:想通了。在主 nginx.conf 文件(/etc/nginx/nginx.conf)中,我将 keepalive_timeout 设置为 65,这被认为太多了。我把它改成0,一切正常。

抱歉,我花了几分钟思考这个问题,然后意识到有更多评论,然后意识到OP确实找到了解决方案:(并且没有将其标记为已回答。