app_test.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env/python
  2. # -*- coding:utf-8 -*-
  3. import pickle
  4. from gevent import monkey
  5. monkey.patch_all() # 打上猴子补丁
  6. from flask import Flask, render_template, send_from_directory
  7. from flask import request, redirect, Response
  8. from flask_cors import *
  9. from multiprocessing import Process, Queue
  10. from gevent import pywsgi
  11. import configs
  12. from structure.danti_structure import single_parse
  13. from structure.structure_main import WordParseStructure
  14. import os, datetime, hashlib
  15. import time, json, random
  16. import pprint
  17. from utils.ruku_opera import Ruku
  18. from pprint import pprint
  19. logger = configs.myLog(__name__, log_cate="reparse_ruku_log").getlog()
  20. app = Flask(__name__)
  21. app.debug = True
  22. CORS(app, supports_credentials=True)
  23. @app.route('/danti_structure', methods=["GET", "POST"])
  24. def danti_structure():
  25. """
  26. 单题再解析、结构化
  27. :return:
  28. """
  29. word_id = request.json.get("paper_id", 0)
  30. one_item = request.json.get("single_item_data", "")
  31. item_type = request.json.get("item_type", "")
  32. logger.info("【单题解析】==request.POST.dict==>word_id:{}, item_type:{}".format(word_id, item_type))
  33. # logger.info("【单题解析】==request.POST.single_item_data==>\n{}\n".format(one_item))
  34. print(word_id, item_type)
  35. res = {"errcode": 0, "errmsgs":"", "data": {}}
  36. if item_type:
  37. one_res = single_parse(one_item, item_type, word_id)
  38. if type(one_res) == str:
  39. res["errcode"] = 1
  40. res["errmsgs"] = one_res
  41. else:
  42. res["data"] = one_res
  43. else:
  44. res["errcode"] = 1
  45. res["errmsgs"] = "没有选定题型"
  46. return json.dumps(res, ensure_ascii=False)
  47. @app.route('/ser_static/<path:file_path>', methods=["GET"])
  48. def ser_static(file_path): # endpoint的位置是函数接口名,不能用static,与flask内部变量重名
  49. """
  50. :param file_path: 图片的本地绝对路径
  51. :return:
  52. """
  53. return send_from_directory(configs.IMG_FOLDER, file_path)
  54. def start_new_server(port):
  55. server = pywsgi.WSGIServer(('',port),app)
  56. server.start_accepting()
  57. if __name__ == "__main__":
  58. # app.run(host=configs.server_ip, port=configs.server_port, processes=True, debug=True) # threaded=True
  59. # app.run(processes=4) # 多进程或多线程只能选择一个,不能同时开启
  60. # 5fc64a0a4994183dda7e74b9
  61. from multiprocessing import cpu_count, Process
  62. # app.debug = True
  63. # server = pywsgi.WSGIServer((configs.server_ip, configs.server_port), app)
  64. # server.serve_forever()
  65. for i in range(cpu_count()):
  66. p = Process(target=start_new_server)
  67. p.start()