当我添加巨大的站点地图时,我的django网站无法加载

时间:2019-07-17 14:58:50

标签: django nginx gunicorn sitemap

我使用django创建了一个大约200万页面的网站。当我使用没有站点地图的gunicorn运行它时,它可以正常工作,但是当我将站点地图添加到项目中时,它不再起作用。 这是我的站点地图生成器:

from django.contrib.sitemaps import Sitemap
from django.contrib.sites.models import Site

from .models import Message

import math


def build_sitemaps():
    sitemaps = {}
    msg_count = Message.objects.count()
    msg_count = int(math.ceil(msg_count/(5*10**4)))
    for i in range(msg_count):
        message_sitemap = MessageSitemap(sitemap_number=i)
        sitemaps[str(i)] = message_sitemap
    return sitemaps


class MessageSitemap(Sitemap):
    changefreq = 'daily'
    priority = 0.5
    limit = 50000
    protocol = 'https'

    def get_urls(self, page=1, site=None, protocol=None):
        fake_site = Site.objects.get(domain='example.com')
        return super(MessageSitemap, self).get_urls(page, site=fake_site, protocol=protocol)

    def __init__(self, sitemap_number):
        self.sitemap_number = sitemap_number
        super(MessageSitemap, self).__init__()

    def items(self):
        return Message.objects.all().order_by('id')

这是我的网址:

from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from django.contrib.sitemaps import views

from telegram_channels.sitemaps import build_sitemaps
from telesearch import settings


urlpatterns = [
    path('admin/', admin.site.urls),
    path('sitemap.xml', views.index, {'sitemaps': build_sitemaps()}),
    path('sitemap-<section>.xml', views.sitemap, {'sitemaps': build_sitemaps()}, name='django.contrib.sitemaps.views.sitemap'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \
    + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

我通过此命令使用gunicorn

gunicorn --certfile=/etc/letsencrypt/live/example.com/fullchain.pem --keyfile=/etc/letsencrypt/live/example.com/privkey.pem -k gevent -b :8000 telesearch.wsgi

有时我会收到此错误:

[2019-07-17 16:48:01 +0200] [8970] [INFO] Listening at: https://0.0.0.0:8000 (8970)
[2019-07-17 16:48:01 +0200] [8970] [INFO] Using worker: gevent
[2019-07-17 16:48:01 +0200] [8974] [INFO] Booting worker with pid: 8974
[2019-07-17 16:48:49 +0200] [8970] [CRITICAL] WORKER TIMEOUT (pid:8974)
[2019-07-17 16:48:50 +0200] [9068] [INFO] Booting worker with pid: 9068

有时会得到这个:

Traceback (most recent call last):
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 66, in handle
six.reraise(*sys.exc_info())
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/six.py", line 625, in reraise
raise value
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/workers/base_async.py", line 49, in handle
req = six.next(parser)
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/http/parser.py", line 41, in __next__
self.mesg = self.mesg_class(self.cfg, self.unreader, self.req_count)
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/http/message.py", line 181, in __init__
super(Request, self).__init__(cfg, unreader)
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/http/message.py", line 54, in __init__
unused = self.parse(self.unreader)
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/http/message.py", line 193, in parse
self.get_data(unreader, buf, stop=True)
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/http/message.py", line 184, in get_data
data = unreader.read()
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/http/unreader.py", line 38, in read
d = self.chunk()
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gunicorn/http/unreader.py", line 65, in chunk
return self.sock.recv(self.mxchunk)
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gevent/_ssl3.py", line 491, in recv
return self.read(buflen)
  File "/mnt/HC_Volume_2811689/<project-location>/env/lib/python3.7/site-packages/gevent/_ssl3.py", line 328, in read
return self._sslobj.read(len or 1024)
OSError: [Errno 0] Error

该项目在本地与runserver正常运行。我猜项目在站点地图中没有任何语法错误。

0 个答案:

没有答案