2013-03-01 72 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

希望這有助於其他人誰過這個問題絆倒。