6

ユーザー許可ポリシーに従ってGETおよびPOST操作を可能にするTastypieリソースを実装しようとしています。モデルは非常に単純であり(TastypieドキュメントのNoteモデルと同様)、リソース自体も非常に単純です。 Haystackで検索を実装するための追加のoverride_urlsメソッド。

現在の私の主な問題は、プロジェクトをローカルで実行することは正常に機能しているように見えますが、要求は高速ですべてであるということです。プロジェクトをデプロイすると(Linodeで、Nginx、Gunicorn、Runitを使用)、POSTリクエストが遅すぎて、201ステータスに戻るのに約1.1分かかることがわかりました。一方、GETリクエストは、期待どおりに機能しています。

リクエストに対してPythonHotshotプロファイラーを実行しましたが、POSTリクエスト全体に0.127CPU秒かかることが示されています。ここで何が起こっているのかよくわかりません。

TastypieリソースにApiKeyAuthenticationとDjangoAuthorizationを使用していることに言及する必要があります。

リクエストに対するChromeInspectorのスクリーンショットは次のとおりです: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 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/;
        }
}
4

2 に答える 2

3

OP: わかった。メインの nginx.conf ファイル (/etc/nginx/nginx.conf) で、keepalive_timeout を 65 に設定していたことがわかりました。これは長すぎると考えられます。私はそれを0に切り替え、すべてがうまくいきました。

申し訳ありませんが、この質問に数分費やした後、さらにコメントがあることに気付き、OPが解決策を見つけたことに気付きました:(そして回答済みとしてマークしませんでした。

于 2013-03-25T07:09:09.693 に答える