john 2 年 前
コミット
c031d31cf5

+ 2 - 0
admin_site/.gitignore

@@ -0,0 +1,2 @@
+results.sqlite
+mysite

+ 5 - 2
admin_site/admin_site/__init__.py

@@ -1,2 +1,5 @@
-# import pymysql
-# pymysql.install_as_MySQLdb()
+# This will make sure the app is always imported when
+# Django starts so that shared_task will use this app.
+from .celery import app as celery_app
+
+__all__ = ('celery_app',)

+ 22 - 0
admin_site/admin_site/celery.py

@@ -0,0 +1,22 @@
+import os
+
+from celery import Celery
+
+# Set the default Django settings module for the 'celery' program.
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'admin_site.settings')
+
+app = Celery('admin_site')
+
+# Using a string here means the worker doesn't have to serialize
+# the configuration object to child processes.
+# - namespace='CELERY' means all celery-related configuration keys
+#   should have a `CELERY_` prefix.
+app.config_from_object('django.conf:settings', namespace='CELERY')
+
+# Load task modules from all registered Django apps.
+app.autodiscover_tasks()
+
+
+@app.task(bind=True, ignore_result=True)
+def debug_task(self):
+    print(f'Request: {self.request!r}')

+ 18 - 9
admin_site/admin_site/settings.py

@@ -15,7 +15,6 @@ from pathlib import Path
 # Build paths inside the project like this: BASE_DIR / 'subdir'.
 BASE_DIR = Path(__file__).resolve().parent.parent
 
-
 # Quick-start development settings - unsuitable for production
 # See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
 
@@ -27,7 +26,7 @@ DEBUG = True
 
 ALLOWED_HOSTS = ['*']
 
-APPEND_SLASH=False
+APPEND_SLASH = False
 
 # Application definition
 
@@ -35,6 +34,8 @@ INSTALLED_APPS = [
     'user',
     'order',
     'files',
+    'django_celery_beat',
+    'django_celery_results',
     'corsheaders',
     'rest_framework',
     'django.contrib.admin',
@@ -43,6 +44,7 @@ INSTALLED_APPS = [
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
+
 ]
 
 MIDDLEWARE = [
@@ -76,7 +78,6 @@ TEMPLATES = [
 
 WSGI_APPLICATION = 'admin_site.wsgi.application'
 
-
 # Database
 # https://docs.djangoproject.com/en/dev/ref/settings/#databases
 
@@ -88,8 +89,8 @@ DATABASES = {
         # 'PASSWORD': '', # 密码
         # 'HOST': '127.0.0.1', # mysql服务所在的主机ip
         # 'PORT': '3306', # mysql服务端口
-        'ENGINE': 'django.db.backends.sqlite3', # 数据库引擎
-        'NAME': BASE_DIR / 'mysite', # 数据库名,先前创建的
+        'ENGINE': 'django.db.backends.sqlite3',  # 数据库引擎
+        'NAME': BASE_DIR / 'mysite',  # 数据库名,先前创建的
         # 'USER': 'root', # 用户名,可以自己创建用户
         # 'PASSWORD': '132546tt', # 密码
         # 'HOST': 'mysql://gz-cdb-pb7zuqlf.sql.tencentcdb.com', # mysql服务所在的主机ip
@@ -97,7 +98,6 @@ DATABASES = {
     }
 }
 
-
 # Password validation
 # https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators
 
@@ -116,7 +116,6 @@ AUTH_PASSWORD_VALIDATORS = [
     },
 ]
 
-
 # Internationalization
 # https://docs.djangoproject.com/en/dev/topics/i18n/
 
@@ -128,7 +127,6 @@ USE_I18N = True
 
 USE_TZ = True
 
-
 # Static files (CSS, JavaScript, Images)
 # https://docs.djangoproject.com/en/dev/howto/static-files/
 
@@ -140,4 +138,15 @@ STATIC_URL = 'static/'
 DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
 
 # 允许全部来源
-CORS_ORIGIN_ALLOW_ALL  = True  # 如果为True,将不使用白名单,并且将接受所有来源。默认为False。
+CORS_ORIGIN_ALLOW_ALL = True  # 如果为True,将不使用白名单,并且将接受所有来源。默认为False。
+
+# Celery settings
+
+CELERY_BROKER_URL = 'redis://localhost:6379/1'
+# CELERY_BROKER_URL = BASE_DIR / 'mysite',  # 数据库名,先前创建的
+# 'NAME': BASE_DIR / 'mysite',  # 数据库名,先前创建的
+#: Only add pickle to this list if your broker is secured
+#: from unwanted access (see userguide/security.html)
+CELERY_ACCEPT_CONTENT = ['json']
+CELERY_RESULT_BACKEND = 'db+sqlite:///results.sqlite'
+CELERY_TASK_SERIALIZER = 'json'

+ 6 - 0
admin_site/files/tasks.py

@@ -0,0 +1,6 @@
+from celery import shared_task
+
+
+@shared_task
+def add(x, y):
+    return x + y

+ 3 - 1
admin_site/files/urls.py

@@ -5,6 +5,8 @@ from . import views
 
 urlpatterns = [
     # re_path('', views.info),
-    re_path('', views.index),
+
     re_path('^info', views.info),
+    re_path('^hhhh', views.test),
+    re_path('', views.index),
 ]

+ 12 - 2
admin_site/files/views.py

@@ -4,8 +4,8 @@ from admin_site.utils import setError, setSuccess, userPersonToDictionary,is_tok
 from admin_site.decorator import my_decorator
 from django.contrib.auth.decorators import login_required
 import os
-from django.http import HttpResponse, Http404, StreamingHttpResponse
-
+from django.http import HttpResponse, Http404, StreamingHttpResponse,JsonResponse
+from .tasks import add
 
 @csrf_exempt
 @require_http_methods(["POST", "GET"])
@@ -36,3 +36,13 @@ def index(request):
 @require_http_methods(["POST", "GET"])
 def info(request):
     return HttpResponse(setError("文件不存在 info"))
+
+@csrf_exempt
+@require_http_methods(["POST", "GET"])
+def test(request):
+    # result = waste_time.delay(2, 3)
+    # return HttpResponse(result.task_id)
+    # return HttpResponse(result.task_id)
+    # return HttpResponse(setError("文件不存在 info"))
+    result = add.delay(1, 2)
+    return HttpResponse(result.get())

BIN
admin_site/mysite


+ 3 - 0
admin_site/requirements.txt

@@ -67,3 +67,6 @@ wadllib==1.3.6
 xdg==5
 xkit==0.0.0
 zipp==1.0.0
+
+celery~=5.2.7
+redis~=4.5.4