使用pycharm运行flask的monkey patch(猴子补丁)时,调试和运行代码报错:
Exception happened during processing of request from ('127.0.0.1', 53647)
Traceback (most recent call last):
File "C:\Python36\lib\socketserver.py", line 639, in process_request_thread
self.finish_request(request, client_address)
File "C:\Python36\lib\socketserver.py", line 361, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python36\lib\socketserver.py", line 696, in __init__
self.handle()
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 325, in handle
rv = BaseHTTPRequestHandler.handle(self)
File "C:\Python36\lib\http\server.py", line 418, in handle
self.handle_one_request()
File "C:\Python36\lib\site-packages\werkzeug\serving.py", line 356, in handle_one_request
self.raw_requestline = self.rfile.readline()
File "C:\Python36\lib\socket.py", line 586, in readinto
return self._sock.recv_into(b)
File "C:\Python36\lib\site-packages\gevent\_socket3.py", line 384, in recv_into
self._wait(self._read_event)
File "C:\Python36\lib\site-packages\gevent\_socket3.py", line 156, in _wait
self.hub.wait(watcher)
File "C:\Python36\lib\site-packages\gevent\hub.py", line 651, in wait
result = waiter.get()
File "C:\Python36\lib\site-packages\gevent\hub.py", line 898, in get
return self.hub.switch()
File "C:\Python36\lib\site-packages\gevent\hub.py", line 630, in switch
return RawGreenlet.switch(self)
gevent.hub.LoopExit: ('This operation would block forever',
----------------------------------------
代码实例:
from flask import Flask from gevent import monkey from gevent import pywsgi import pymysql monkey.patch_all(thread=False) # 使用猴子补丁,将真个应用转变为协程的方式运行,提高并发性能,thread=False使多线程不阻塞 app = Flask(__name__) @app.route('/t') def hello_world(): return 'Hello World!' if __name__ == '__main__': #app.run() server = pywsgi.WSGIServer(("127.0.0.1", 5000), app) # 使用WSGIServer将app应用封装成wsgi服务 server.serve_forever()
原因分析:
查看代码没有配置和编辑错误
查看flask启动命令打印:
C:\Python36\python.exe -m flask run
* Serving Flask app "main.py"
* Environment: development
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
发现启动的命令不对
根因分析:
配置运行的命令不对,这是默认flask启动的配置
解决方式:
修改flask启动环境及命令配置
如上为正常启动右键格式,打印的执行命令应该如下: