我正在使用DRF(Django REST框架),但有2个问题。我正在使用Vagrant VM(Windows 10)。 我已经配置了一些缓存功能和“延迟”加载。例如:
...
@method_decorator(vary_on_cookie)
@method_decorator(cache_page(60*60*1))
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
queryset = my_object.objects.all().order_by('begin')
# Set up eager loading to avoid N+1 selects
queryset = self.get_serializer_class().setup_eager_loading(self,queryset)
return queryset
...
1-随机地,我有一些错误消息,例如:
-----> calling API : http://localhost:8000/my_app/highway/2
('Environment(Config) Error ! => ', ConnectionError(ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',)),), <class 'requests.exceptions.ConnectionError'>)
经过一些研究,我发现没有什么可以帮助我:(。
2-在循环中运行api调用是一种不好的方法吗? :
# check if there's a 'xxxx'
if len(prpk['zone']['com']) > 0:
# get the zone of the 'com'(s)
for com in prpk['zone']['com']:
zones_by_com = tools_cronjob.getAPI(API_URL + actions_api['zones_by_com'] + "/%s" % com['id'], headers)
# time.sleep(5)
print (zones_by_com)
...
def getAPI(url, header):
datas = []
if len(url) >0 and len(header) > 0:
print ('-----> calling API : %s' % url)
response = requests.get(url, headers=header)
if response.status_code == 200:
datas = json.loads(response.content.decode('utf-8'))
else:
raise NotImplementedError('<!> (getAPI) API url (%s) failed ! (%s)' % (url,response.status_code))
else:
raise NotImplementedError('<!> (getAPI) API url (%s) failed ! ' % url)
return datas
请问有人有主意吗?
谢谢。 F。