2013-03-01 68 views
6

我试图实现一个Tastypie资源,允许按照每个用户权限策略进行POST操作,该模型非常简单(类似于Tastypie文档中的Note模型)和资源本身也很简单,我只需要一个额外的override_urls方法来实现Haystack的搜索。Django Tastypie缓慢POST响应

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

我对请求运行了一个Python Hotshot分析器,它显示整个POST请求需要0.127 CPU秒。我不确定这里发生了什么。

我应该提到我对我的Tastypie资源使用了ApiKeyAuthentication和DjangoAuthorization。

下面是Chrome检查截图的请求: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) 

Gunicorn CONF:

bind = "0.0.0.0:1330" 
workers = 1 

Nginx的配置(包括在主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/; 
     } 
} 
+0

向我们展示我的代码 – 2013-03-01 20:41:36

+0

@Hedde编辑帖子 – 2013-03-01 20:58:57

+0

礼貌通常表明您应该确定您已经在Google群组中发布了这条消息。你有没有在模型上设置任何信号处理规则?特别是post_save? – 2013-03-02 01:36:17

回答

3

OP:算出来。在主要的nginx.conf文件(/etc/nginx/nginx.conf)中,我发现keepalive_timeout被设置为65,这被认为是太多了。我把它切换到0,一切正常。

对不起,我花了一两分钟,这个问题,然后意识到有更多的意见,然后实现了OP没有找到一个解决办法:(并没有将其标记为回答。

0

虽然改变keepalive_timeout会。工作,它不修复的nginx的Content-Length头错误,导致它根本此错误是fixed in version 0.8.32 of nginx,但如果你有一个旧的版本,你可以:

  1. 更改服务器
  2. 上keepalive_timeout = 0
  3. 将服务器上的nginx升级至版本> = 0.8.32
  4. 修复了在服务器端代码解释here

希望这有助于其他人谁过这个问题绊倒。