windows环境:Apache + flask,遇到的一些问题

Q1

记录:flask中应用多进程的话,会导致多个httpd进程,进而Apache服务不能正常运行。这个在Windows上还没找到合适的解决方式。归其原因,Python中多线程实现中存在GIL,导致Python在运行多线程时也只是调用一个核心的资源。因此,用Python实现多线程时,如果是计算密集型任务性能反而会下降。

  • 如果只是功能上实现,不需要Apache上部署,可以采用Python多进程的方式调用多个核心的计算资源;
  • 如果需要部署,那么可以采取其他措施绕过GIL,例如采用ctype调用C的动态链接库。

Q2

Apache监听多个端口出现:RuntimeError: implement_array_function method already has a docstring错误详情如下:

Wed May 29 12:25:43.645031 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242] mod_wsgi (pid=12696): Target WSGI script 'D:/SJC_CM/train_model.wsgi' cannot be loaded as Python module.
[Wed May 29 12:25:43.645031 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242] mod_wsgi (pid=12696): Exception occurred processing WSGI script 'D:/SJC_CM/train_model.wsgi'.
[Wed May 29 12:25:43.645031 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242] Traceback (most recent call last):\r
[Wed May 29 12:25:43.645031 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:/SJC_CM/train_model.wsgi", line 9, in \r
[Wed May 29 12:25:43.645031 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     from train_model import app as application\r
[Wed May 29 12:25:43.645031 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\train_model.py", line 12, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     from keras import backend as K\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\venv\\Lib\\site-packages\\keras\\__init__.py", line 3, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     from . import utils\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\venv\\Lib\\site-packages\\keras\\utils\\__init__.py", line 2, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     from . import np_utils\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\venv\\Lib\\site-packages\\keras\\utils\\np_utils.py", line 6, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     import numpy as np\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\venv\\Lib\\site-packages\\numpy\\__init__.py", line 142, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     from . import core\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\venv\\Lib\\site-packages\\numpy\\core\\__init__.py", line 40, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     from . import multiarray\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\venv\\Lib\\site-packages\\numpy\\core\\multiarray.py", line 12, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     from . import overrides\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]   File "D:\\SJC_CM\\venv\\Lib\\site-packages\\numpy\\core\\overrides.py", line 46, in \r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242]     """)\r
[Wed May 29 12:25:43.646007 2019] [wsgi:error] [pid 12696:tid 888] [client 127.0.0.1:61242] RuntimeError: implement_array_function method already has a docstring\r

这个问题有时出现,很奇怪。之前是没有出现的,换台电脑就出现了这种情况。解决方法如下:
首先找到Apache的httpd.conf文件,添加WSGIApplicationGroup %{GLOBAL}

捕获.PNG

保存conf文件,重启Apache服务,应该可以解决问题。


Q3

flask传递json时,中文无法正常显示,解决方案:

return json.dumps(return_js, ensure_ascii=False)

在json.dumps()中加上条件:ensure_ascii=False

你可能感兴趣的:(windows环境:Apache + flask,遇到的一些问题)