日萌社
人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度學習實戰(不定時更新)
2.1 Web接口業務流介紹
學習目標
- 目標
- 知道用戶推薦接口運行流程
- 應用
- 無
2.1.1 Web端環境啓動
我們這裏使用supervisor進程管理工具進行管理Web服務的啓動,nginx+uwsgi(django)是web端的服務解決方案
2.1.1.1 supervisor啓動
配置supervisor要啓動的進程服務:3個服務
- 1、uwsgi服務
[program:main_server]
command=uwsgi --ini /home/zhoumingzhen/conf/uwsgi.ini --close-on-exec
stopsignal=QUIT ; signal used to kill process (default TERM)
stopasgroup=false ; send stop signal to the UNIX process group (default false)
killasgroup=false ; SIGKILL the UNIX process group (def false)
stdout_logfile=/home/zhoumingzhen/log/main_server_out.log
stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile=/home/zhoumingzhen/log/main_server_err.log
stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
- 2、啓動nginx的web服務,在start_nginx.ini啓動配置中
[program:nginx]
command=/usr/sbin/nginx -c /home/zhoumingzhen/conf/nginx/nginx.conf -g "daemon off;"
stdout_logfile=/home/zhoumingzhen/log/nginx_out.log
stderr_logfile=/home/zhoumingzhen/log/nginx_err.log
stdout_logfile_maxbytes=1MB
stderr_logfile_maxbytes=1MB
- 3、啓動redis相關服務
[program:redis]
command=redis-server /home/zhoumingzhen/conf/redis.conf
stdout_logfile=/home/zhoumingzhen/log/redis_out.log
stderr_logfile=/home/zhoumingzhen/log/redis_err.log
stdout_logfile_maxbytes=1MB
stderr_logfile_maxbytes=1MB
2.1.2 後臺業務邏輯
圍繞着APP設計中,用戶可能進行的那些操作,以及在什麼地方提供對用戶的推薦接口。
-
API總覽:
- 用於用戶獲取推薦:
- 首次推薦API
- 個性化推薦API
- 推薦結果操作行爲API:
- 用戶點贊API
- 用戶評論API
- 用戶轉發API
- 用戶取消點贊API
- 用戶刪除評論API
- 用於用戶獲取推薦:
-
視圖函數邏輯
相關django框架模塊,以及推薦模塊導入
from django.http import HttpResponse
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import authentication_classes
from rest_framework.decorators import permission_classes
from . import api
from recomm import api as r_api
import json
import logging
- 實現路由:
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^api/first_show[/]?$', views.first_show), # 用戶第一次請求
url(r'^api/get_cache[/]?$', views.get_cache), # 用戶請求,獲取緩存結果
url(r'^api/like[/]?$', views.like),
url(r'^api/forward[/]?$', views.forward),
url(r'^api/commend[/]?$', views.commend)
url(r'^api/cancel_like[/]?$', views.cancel_like),
url(r'^api/delete_commend[/]?$', views.delete_commend)
]
2.1.2.1 接口演示
訪問對應接口可以獲得推薦結果:
[{"hot_score": 0, "text_info": "😉", "iv_url": "http://p.upcdn.pengpengla.com/star/p/u/2017/8/10/2f452e28-3e63-48ed-87e3-b7e4ad508785.jpg", "publish_time": 1502339444, "commented_num": 0, "liked_num": 0, "forwarded_num": 0, "pid": 675117, "related_stars_list": ["2", "4", "5"]}, {"hot_score": 71, "text_info": "不願讓暴暴受傷害的鏟屎官……", "iv_url": "http://p.upcdn.pengpengla.com/star/p/u/2017/7/4/63563717-4e4d-4500-8bf3-5d4e332132fd.png", "publish_time": 1499161824, "commented_num": 0, "liked_num": 71, "pid": 690410, "forwarded_num": 0, "related_stars_list": ["2", "4", "5"]}, {"hot_score": 108, "text_info": "這幾張最帥好嗎", "iv_url": "http://p.upcdn.pengpengla.com/star/p/u/2017/8/7/a97755a5-6867-49bf-8525-ab932fcc2006.jpg", "publish_time": 1502088406, "commented_num": 0, "liked_num": 108, "pid": 674069, "forwarded_num": 0, "related_stars_list": ["2", "4", "5"]}, {"hot_score": 72, "text_info": "大愛謙謙", "iv_url": "http://p.upcdn.pengpengla.com/star/p/u/2017/8/7/795b5e28-ca55-477a-80be-321748f01aa1.jpg", "publish_time": 1502090884, "commented_num": 0, "liked_num": 72, "pid": 674085, "forwarded_num": 0, "related_stars_list": ["2", "4", "5"]}, {"hot_score": 20, "text_info": "李東學語音素材第一版回顧", "iv_url": "http://g.cdn.pengpengla.com/starfantuan/fanquan/149760691453.mp3", "publish_time": 1497606612, "commented_num": 2, "liked_num": 16, "pid": 260409, "forwarded_num": 0, "related_stars_list": ["2", "4", "5"]}]
2.1.2.2 用戶推薦接口介紹
主要由兩個通過用戶首次推薦和個性化推薦的接口
- first_show
用戶第一次進行請求,獲取熱門召回推薦
@api_view(['GET', 'POST'])
def first_show(request):
IP = request.META.get("HTTP_X_REAL_IP")
result = r_api.get_hot(str(IP))
return HttpResponse(json.dumps(result, ensure_ascii=False))
- get_cache
根據用戶請求,獲取用戶緩存結果(進行用戶是否第一次使用某IP登陸判斷)
@api_view(['GET', 'POST'])
def get_cache(request):
IP = request.META.get("HTTP_X_REAL_IP")
result = r_api.v_get_cache(str(IP))
return HttpResponse(json.dumps(result, ensure_ascii=False))
- get_recomm
獲取用戶推薦結果(直接獲取一次召回推薦結果)
@api_view(['GET', 'POST'])
def get_recomm(request):
IP = request.META.get("HTTP_X_REAL_IP")
result = r_api._get_recomm(str(IP))
return HttpResponse(json.dumps(result, ensure_ascii=False))
其中都會通過from recomm import api as r_api這個包的相關函數進行推薦,我們推薦邏輯主要都在recomm模塊中,這是自定義命名的,當做推薦模塊使用。
2.1.3 小結
- Web端環境啓動
- uwsgi服務
- nginx服務
- redis服務
- 用戶推薦接口
- first_show、get_cache