我正在尝试实现一个 Tastypie 资源,它允许按照每个用户权限策略进行 GET 和 POST 操作,模型非常简单(类似于 Tastypie 文档中的 Note 模型),资源本身也非常简单,我只是有一个额外的 override_urls 方法来实现使用 Haystack 进行搜索。
我现在的主要问题是,虽然在本地运行项目似乎工作正常,但请求速度很快,而且一切正常。一旦我部署了项目(在 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)
独角兽会议:
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/;
}
}