Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slow Serializion process Django rest framework

Im using django_rest_framework for my project and i have a problem.

models.py:

from django.db import models


class RelatedField3_2(models.Model):
    name = models.CharField(max_length=100)
    def __str__(self):
        return self.name


class RelatedField3(models.Model):
    name = models.CharField(max_length=100)
    relfield3_2 = models.ForeignKey(RelatedField3_2, on_delete=models.CASCADE)
    def __str__(self):
        return self.name


class RelatedField2(models.Model):
    name = models.CharField(max_length=100)
    def __str__(self):
        return self.name


class RelatedField1(models.Model):
    name = models.CharField(max_length=200)
    def __str__(self):
        return self.name

class MainTable(models.Model):
    owner = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    field1 = models.IntegerField()
    field2 = models.IntegerField()
    field3 = models.IntegerField()
    field4 = models.DecimalField(max_digits=10, decimal_places=1)
    field5 = models.IntegerField(null=True)
    field6 = models.IntegerField()
    relfield1 = models.ForeignKey(RelatedField1, on_delete=models.CASCADE)
    relfield2 = models.ForeignKey(RelatedField2, on_delete=models.CASCADE)
    relfield3 = models.ForeignKey(RelatedField3, on_delete=models.CASCADE)

serializers.py:

from rest_framework import serializers
from main.models import MainTable, RelatedField1, RelatedField2, RelatedField3


class MainTableRelatedFields(serializers.RelatedField):
    def display_value(self, instance):
        return instance

    def to_representation(self, value):
        return str(value)

    def to_internal_value(self, data):
        return self.queryset.model.objects.get(name=data)


class MainTableSerializerList(serializers.ListSerializer):

    def create(self, validated_data):
        records = [MainTable(**item) for item in validated_data]
        return self.child.Meta.model.objects.bulk_create(records)


class MainTableSerializer(serializers.ModelSerializer):
    class Meta:
        model = MainTable
        list_serializer_class = MainTableSerializerList

    id = serializers.IntegerField(write_only=False, required=False)
    owner = serializers.ReadOnlyField(source='owner.username')
    relfield3 = PartnerPropRelatedFields(queryset=RelatedField3.objects.all())
    relfield2 = PartnerPropRelatedFields(queryset=RelatedField2.objects.all())
    relfield1 = PartnerPropRelatedFields(queryset=RelatedField1.objects.all())

    def create(self, validated_data):
        return self.Meta.model.objects.create(**validated_data)

views.py:

from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticated

from .serializers import MainTableSerializer


class MainTableUploadView(viewsets.ModelViewSet):
    permission_classes = (IsAuthenticated,)

    @action(['post'], detail=False)
    def upload_records(self, request, *args, **kwargs):
        seriazlizer = MainTableSerializer(data=request.data, many=True)
        seriazlizer.is_valid(raise_exception=True)
        seriazlizer.save(owner=request.user)
        return Response(seriazlizer.data)

send_json.py:

import requests

data_load_list = [{    
        "field1": 1,
        "field2": 2,
        "field3": 3,
        "field4": "field4",
        "field5": 5,
        "field6": "field6",
        "relfield1": "test1",
        "relfield2": "test2",
        "relfield3" : "test3"
    } for i in range(1000)
]

load_list_response = requests.post(url=url_upload, headers={'Authorization': f'Token {TOKEN}'}, 
                                   json=data_load_list)

When i start send_json.py for sending 1000 records, i just get error:

RemoteDisconnected                        Traceback (most recent call last)
~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    669             # Make the request on the httplib connection object.
--> 670             httplib_response = self._make_request(
    671                 conn,

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    425                     # Otherwise it looks like a bug in the code.
--> 426                     six.raise_from(e, None)
    427         except (SocketTimeout, BaseSSLError, SocketError) as e:

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/packages/six.py in raise_from(value, from_value)

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    420                 try:
--> 421                     httplib_response = conn.getresponse()
    422                 except BaseException as e:

/usr/lib/python3.8/http/client.py in getresponse(self)
   1331             try:
-> 1332                 response.begin()
   1333             except ConnectionError:

/usr/lib/python3.8/http/client.py in begin(self)
    302         while True:
--> 303             version, status, reason = self._read_status()
    304             if status != CONTINUE:

/usr/lib/python3.8/http/client.py in _read_status(self)
    271             # sending a valid response.
--> 272             raise RemoteDisconnected("Remote end closed connection without"
    273                                      " response")

RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

ProtocolError                             Traceback (most recent call last)
~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    438             if not chunked:
--> 439                 resp = conn.urlopen(
    440                     method=request.method,

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    725 
--> 726             retries = retries.increment(
    727                 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    402             if read is False or not self._is_method_retryable(method):
--> 403                 raise six.reraise(type(error), error, _stacktrace)
    404             elif read is not None:

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/packages/six.py in reraise(tp, value, tb)
    733             if value.__traceback__ is not tb:
--> 734                 raise value.with_traceback(tb)
    735             raise value

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    669             # Make the request on the httplib connection object.
--> 670             httplib_response = self._make_request(
    671                 conn,

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    425                     # Otherwise it looks like a bug in the code.
--> 426                     six.raise_from(e, None)
    427         except (SocketTimeout, BaseSSLError, SocketError) as e:

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/packages/six.py in raise_from(value, from_value)

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    420                 try:
--> 421                     httplib_response = conn.getresponse()
    422                 except BaseException as e:

/usr/lib/python3.8/http/client.py in getresponse(self)
   1331             try:
-> 1332                 response.begin()
   1333             except ConnectionError:

/usr/lib/python3.8/http/client.py in begin(self)
    302         while True:
--> 303             version, status, reason = self._read_status()
    304             if status != CONTINUE:

/usr/lib/python3.8/http/client.py in _read_status(self)
    271             # sending a valid response.
--> 272             raise RemoteDisconnected("Remote end closed connection without"
    273                                      " response")

ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

ConnectionError                           Traceback (most recent call last)
<timed exec> in <module>

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/api.py in post(url, data, json, **kwargs)
    117     """
    118 
--> 119     return request('post', url, data=data, json=json, **kwargs)
    120 
    121 

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/api.py in request(method, url, **kwargs)
     59     # cases, and look like a memory leak in others.
     60     with sessions.Session() as session:
---> 61         return session.request(method=method, url=url, **kwargs)
     62 
     63 

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    528         }
    529         send_kwargs.update(settings)
--> 530         resp = self.send(prep, **send_kwargs)
    531 
    532         return resp

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
    641 
    642         # Send the request
--> 643         r = adapter.send(request, **kwargs)
    644 
    645         # Total elapsed time of the request (approximately)

~/.local/share/virtualenvs/DKykH6t1/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    496 
    497         except (ProtocolError, socket.error) as err:
--> 498             raise ConnectionError(err, request=request)
    499 
    500         except MaxRetryError as e:

ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

When i start send_json.py for sending 100 records, the data is loaded into the database and returned with a code 200. This takes 15 seconds.

How and with what can I optimize my code so that I can send 1000+ records to the database and get information in the response body quickly?

like image 240
hax Avatar asked Nov 06 '22 05:11

hax


1 Answers

the solution that helped me (see changes in pull request):

https://github.com/hax2000/Relaed-Fields-API/pull/1/files

like image 167
hax Avatar answered Nov 12 '22 19:11

hax