gpt4 book ai didi

python - Django 和 MySQL 需要哪些配置设置来支持并发请求?

转载 作者:行者123 更新时间:2023-11-28 23:44:20 24 4
gpt4 key购买 nike

我创建了一个名为 test123 的项目,下面是 settings.py 和 urls.py:

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'dbuser.cnf'),
}
},
}
root@cdbe25bac912:~/test123/test123# cat urls.py
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
url(r'^myapp/', include('myapp.urls')),
]

我创建了一个名为 myapp 的应用程序。

root@cdbe25bac912:~/test123/myapp# cat models.py
from django.db import models

class Package(models.Model):
name = models.CharField(max_length=50)
desc = models.CharField(max_length=50)
root@cdbe25bac912:~/test123/myapp# cat serializers.py
from myapp.models import *
from rest_framework import serializers

class PackageSerializer(serializers.ModelSerializer):
class Meta:
model = Package
fields = ( 'id', 'name', 'desc',)

root@cdbe25bac912:~/test123/myapp# cat views.py
from django.shortcuts import render
from myapp.models import *
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.response import Response
from myapp.serializers import *
class PackageViewList(APIView):
def get(self, request, format=None):
package = Package.objects.all()
serializer = PackageSerializer(package, many=True)
return Response(serializer.data)

def post(self, request, format=None):
serializer = PackageSerializer(data = request.data)
if serializer.is_valid():
print serializer.validated_data
serializer.save()
return Response(serializer.data, status = status.HTTP_201_CREATED)
return Response(serializer.errors, status = status.HTTP_400_BAD_REQUEST)

root@cdbe25bac912:~/test123/myapp# cat urls.py
from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^package/$', views.PackageViewList.as_view()),
]
root@cdbe25bac912:~/test123/myapp#

以下是使用 Django 测试框架编写的测试脚本,它创建多个 APIClient 对象来模拟多个用户并从这些对象触发 POST 操作。

root@cdbe25bac912:~/test123/myapp# cat tests.py.bkup
from django.test import TestCase
from rest_framework.test import APIClient
from rest_framework.test import APITestCase, APILiveServerTestCase
from myapp.models import Package
from multiprocessing import Process

class ConcurrentTest(APILiveServerTestCase):
def setUp(self):
self.apiclient_list = []
self.num_clients=4

package = Package.objects.create(name='pack333', desc='package333')
package.save()
self.pack = {"name" : "pack", "desc" : "package"}

#create number of APIClient objects and login.
for client_id in range(self.num_clients):
apiclient = APIClient()
self.apiclient_list.append(apiclient)

def post(self, apiclient, client_id, url, data):
print 'Started POST Client ID = %s' % (str(client_id))
data['name'] = 'packpost' + str(client_id)
data['desc'] = 'packdesc' + str(client_id)
print data
response = apiclient.post(url, data, format="json")
self.assertEqual(response.status_code, 201)
print 'Completed POST Client ID = %s' % (str(client_id))
def test_concurrent_restops(self):
"""
Description : Simulate multiple users and issue concurrent REST operations
"""
process_list = []

#Issue concurrent POST operations.
for client_id in range(len(self.apiclient_list)):
t = Process(target=self.post, args=(self.apiclient_list[client_id], client_id, '/myapp/package/', self.pack))
process_list.append(t)

for process in process_list :
process.start()
for process in process_list :
process.join()

root@cdbe25bac912:~/test123/myapp#

问题症状:

当我运行上面的测试问题时:python manage.py test myapp

至少有一个在 test.py 中创建的进程会挂起。我假设 Django 和 MySQL 需要一些配置来支持来自不同用户的并发操作。

最佳答案

看起来 LiveServerTestCase 是单线程的,这可以解释为什么进程/请求挂起,因为服务器一次只能服务一个请求:

https://github.com/django/django/blob/53ccffdb8c8e47a4d4304df453d8c79a9be295ab/django/test/testcases.py#L1319

https://github.com/django/django/blob/53ccffdb8c8e47a4d4304df453d8c79a9be295ab/django/test/testcases.py#L1332

https://github.com/django/django/blob/53ccffdb8c8e47a4d4304df453d8c79a9be295ab/django/test/testcases.py#L1255


我能想到的一件事是您没有看到使用 sqlite 的阻塞是因为 sqlite 在内存中运行以进行 django unittest,大大提高了测试运行的速度。

Django LiveServerTestServer 是一种“玩具”,可能不太适合加载样式测试。 MySQL 应该能够很容易地处理开箱即用的良好水平的并发,但我不确定 LiveServerTestServer 是否可以。

如果您针对现在是多线程(我相信)的 python manage.py runserver 运行您的代码,您应该注意到一个区别。要了解您的应用程序如何处理负载,最好在部署时运行您的代码(使用您的产品网络服务器:apache、uwsgi、gunicorn 等)

关于python - Django 和 MySQL 需要哪些配置设置来支持并发请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33854030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com