在技术横冲直撞、框架满天飞的这个时代,快速运行起一个Web应用已经是零门槛的事情,这篇文章主要记录一下用Python Flask框架来快速构造“HelloWorld”的经历;
既然是基于Python的框架,那么首先默认你已经安装了Python;
笔者在学习的过程中就用了这个链接进行了对Flask的入门学习:http://www.pythondoc.com/flask/quickstart.html#id2,初学者不用太追求过于官方、过于面面俱到的文档,这么说也不是排斥去系统地学习钻研一门技术,而是想说明当新入门一个技术的时候,只有快速的Run起来,快速地看到效果,才能保证自己兴趣的浓厚度,如果一个框架或是一个开源项目,要花费你好半天的时间才能Run,那么可能的情况是你的兴趣已经被磨灭了一半;
用Flask构造一个简单的程序如下:
1. pip安装flask包 :pip install flask
2.你的第一个应用程序基本是这样的:
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' if __name__ == '__main__': app.run()
好了,可以Run了,你会看到如下的输出:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 517-355-210
那么对于有web基础的人来说,已经很明显知道这是干什么了,是的,在本地起了一个5000端口作为http服务,直接在浏览器访问http://127.0.0.1:5000/,
你就能看到以下的页面:
到此为止,一个简单的web程序已经构造完成,虽然里线上生产还是差了很多要素;
接下来再记录几个常用问题:
1. 怎么让网络上的其他人访问我的服务?
app.run(host='0.0.0.0')
只要改成0.0.0.0,你的服务器监控的所有ip地址,即任何人都能访问你的服务啦(前提是你的服务不是在本地跑,而是已经部署到了一台阿里云或者腾讯云或者xx云了)
2.目前Run起来的这个应用是多线程跑的吗?
app.run(threaded=True)
只要程序改成这样,threaded=True,你的程序就是多线程的,反之,则是单线程;
笔者也进行了如下的测试:
场景一:Flask单线程模式代码
from flask import Flask app = Flask(__name__) import threading count = 1 @app.route("/") def hello(): print(threading.current_thread().name) global count for i in range(0, 10000): count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 print("hello:%d\n" % count) return "hello" if __name__ == '__main__': app.run(threaded=False)
Run一下,然后用ab测试命令,去跑一个小的压测:ab -c 10 -n 10 "http://127.0.0.1:5000/,这里是10个进程并发发送10个请求;
输出如下:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
MainThread
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
hello:1
MainThread
hello:1
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
MainThread
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
hello:1
MainThread
hello:1
MainThread
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
hello:1
MainThread
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
hello:1
MainThread
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
hello:1
MainThread
hello:1
MainThread
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
hello:1
MainThread
127.0.0.1 - - [09/Apr/2019 22:30:18] "GET / HTTP/1.0" 200 -
hello:1
可以看到每次都是同一个线程,而每次输出的值也都是hello:1
场景二:Flask多线程模式代码
from flask import Flask app = Flask(__name__) import threading count = 1 @app.route("/") def hello(): print(threading.current_thread().name) global count for i in range(0, 10000): count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 count = count + 1 count = count - 1 print("hello:%d\n" % count) return "hello" if __name__ == '__main__': app.run(threaded=True)
同样Run一下, 用相同ab测试命令去压测,可以看到如下输出:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Thread-1
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
hello:1
Thread-2
hello:1
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
Thread-3
hello:1
Thread-4
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
hello:1
Thread-5
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
hello:1
Thread-6
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
hello:1
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
Thread-7
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
hello:1
Thread-8
hello:1
Thread-9
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
hello:1
Thread-10
127.0.0.1 - - [09/Apr/2019 22:34:58] "GET / HTTP/1.0" 200 -
hello:1
可以看到每次的线程名都不一样,显然开启了多线程的模式;
上述两种场景的唯一区别就在于threaded这个参数的值,可以动手测试一下,一旦涉及到多线程,就会涉及到加锁、线程上下文切换等行为,而这些Flask并没有为我们提供啥方法避免,需要我们按照不同模式下的不同场景去自己编写了;