djangoerstframework(DRF) 访问一直在转,阻塞

发布于 2022-09-11 20:48:46 字数 4978 浏览 14 评论 0

djangorestframework = "==3.9.4"

models.py

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey has `on_delete` set to the desired behavior.
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
import uuid

from django.db import models
from django.utils.translation import ugettext_lazy as _


class Assets(models.Model):
    # Important
    PLATFORM_CHOICES = (
        ('Linux', 'Linux'),
        ('Unix', 'Unix'),
        ('MacOS', 'MacOS'),
        ('BSD', 'BSD'),
        ('Windows', 'Windows'),
        ('Windows2016', 'Windows(2016)'),
        ('Other', 'Other'),
    )

    SSH_PROTOCOL = 'ssh'
    RDP_PROTOCOL = 'rdp'
    TELNET_PROTOCOL = 'telnet'
    PROTOCOL_CHOICES = (
        (SSH_PROTOCOL, 'ssh'),
        (RDP_PROTOCOL, 'rdp'),
        (TELNET_PROTOCOL, 'telnet (beta)'),
    )

    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    ip = models.GenericIPAddressField(max_length=32, verbose_name=_('IP'), db_index=True)
    hostname = models.CharField(max_length=128, verbose_name=_('Hostname'))
    protocol = models.CharField(max_length=128, default=SSH_PROTOCOL, choices=PROTOCOL_CHOICES,
                                verbose_name=_('Protocol'))
    port = models.IntegerField(default=22, verbose_name=_('Port'))
    platform = models.CharField(max_length=128, choices=PLATFORM_CHOICES, default='Linux', verbose_name=_('Platform'))
    is_active = models.BooleanField(default=True, verbose_name=_('Is active'))

    # Auth
    admin_user_id = models.CharField(max_length=128, null=True, verbose_name=_("Admin user"))

    # Some information
    # public_ip = models.GenericIPAddressField(max_length=32, blank=True, null=True, verbose_name=_('Public IP'))
    # number = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Asset number'))

    # Collect
    vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor'))
    model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model'))
    sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number'))

    cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model'))
    cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count'))
    cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores'))
    cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus'))
    memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory'))
    disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total'))
    disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info'))

    os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS'))
    os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version'))
    os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch'))
    hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw'))

    created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
    date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created'))
    comment = models.TextField(max_length=128, default='', blank=True, verbose_name=_('Comment'))

    def __str__(self):
        return '{0.hostname}({0.ip})'.format(self)

    class Meta:
        db_table = "assets"

serializers.py

from rest_framework.serializers import ModelSerializer

from assets.models import Assets


class AssetsSerializer(ModelSerializer):
    class Meta:
        model = Assets
        fields = "__all__"

views.py

from django_filters.rest_framework import DjangoFilterBackend

from rest_framework import generics

from assets.models import Asset
from assets.serializers import AssetSerializer


class AssetList(generics.ListAPIView):
    queryset = Asset.objects.all()
    serializer_class = AssetSerializer
    filter_backends = (DjangoFilterBackend,)
    filterset_fields = "__all__"


class AssetDetail(generics.RetrieveAPIView):
    queryset = Asset.objects.all()
    serializer_class = AssetSerializer

访问100 条, 500ms, 正常

http://localhost:8080/assets/?limit=100&offset=0

访问101条,直接阻塞,360加速器显示使用率96%

http://localhost:8080/assets/?limit=101&offset=0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2022-09-18 20:48:46

找到问题了
环境:win10 wsl ubuntu18.04LTS
在list(Assets.objects.all())时,360加速球显示95%
而资源管理器显示内存95%,cpu在30%-40%浮动,有时候会60%和70%小尖峰
pagination.py

class LimitOffsetPagination(BasePagination):
   ...
   def def paginate_queryset(self, queryset, request, view=None):
       ...
       return list(queryset[self.offset:self.offset + self.limit])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文