day3

[if !supportLists]1 [endif]虚拟环境

[if !supportLists]1.1 [endif]安装虚拟环境

使用Python进行项目开发时,由于不同的项目需要,可能会配置多个开发环境,不同开发环境之间的项目依赖包如果混合在一起,可能会引起意想不到的错误。

通过虚拟环境隔离不同开发环境,方便不同开发环境的共存。

#安装python虚拟环境

sudo apt install -y virtualenv

#vrtaulenvwrapper是virtualenv的扩展包,用于更方便管理虚拟环境

sudo apt install -y virtualenvwrapper

[if !supportLists]1.2 [endif]配置虚拟环境

此时还不能使用virtualenvwrapper,实际上你需要运行virtualenvwrapper.sh文件才行。

配置步骤如下:

1、查看virtualenvwrapper的安装路径

sudo find / -name virtualenvwrapper.sh

2、创建目录用来存放虚拟环境

mkdir ~/.myvirtualenvs

3、在~/.bashrc中添加行

export WORKON_HOME=/home/yong/.myvirtualenvs

source /usr/share/virtualenvwrapper/virtualenvwrapper.sh

4、运行: source ~/.bashrc

此时virtualenvwrapper就可以使用了。

[if !supportLists]1.3 [endif]virtualenvwrapper的功能

配置好virtualenvwrapper,就可以使用它的功能,方便管理虚拟环境

workon:         列出虚拟环境列表

lsvirtualenv: 列出虚拟环境列表

mkvirtualenv: 新建虚拟环境

workon [虚拟环境名称]: 切换/进入虚拟环境

rmvirtualenv :     删除虚拟环境

deactivate: 离开虚拟环境

[if !supportLists]1.4 [endif]创建虚拟环境

进入本地虚拟环境的目录文件夹

cd ~/.myvirtualenvs/

创建虚拟环境 根据需要选择3和2

mkvirtualenv -p /usr/bin/python3  py3

mkvirtualenv -p /usr/bin/python  py2

进入虚拟环境

workon virtualenv-django

查看虚拟环境中已经安装的包

pip list

pip freeze

[if !supportLists]2 [endif]uwsgi

UWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

要注意WSGI / uwsgi / uWSGI这三个概念的区分。

[if !supportLists]Ÿ [endif]WSGI是一种通信协议。

[if !supportLists]Ÿ [endif]uwsgi是一种线路协议而不是通信协议,在此常用于在uWSGI服务器与其他网络服务器的数据通信。

[if !supportLists]Ÿ [endif]而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。

uwsgi协议是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述,它与WSGI相比是两样东西。

[if !supportLists]2.1 [endif]安装

[if !supportLists]Ÿ [endif]安装依赖

sudo apt-get -y install build-essential python3-dev python-pip curl

[if !supportLists]Ÿ [endif]pip命令安装,这里应该在python3的虚拟环境里安装

pip install uwsgi

[if !supportLists]Ÿ [endif]下载安装脚本,将uwsgi二进制安装到家目录下,你可以修改它。

sudo curl http://uwsgi.it/install | bash -s default /home/yong/uwsgi

sudo mv /home/yong/uwsgi /usr/local/sbin/

[if !supportLists]2.2 [endif]使用

[if !supportLists]Ÿ [endif]新建服务wsgi协议的逻辑代码server.py

def application(environ, start_response):

    body = '

Hello,World!

'

    start_response('200 OK', [('Content-Type', 'text/html')])

return body.encode('utf-8')


environ:请求信息组成的字典

start_response:处理响应

[if !supportLists]Ÿ [endif]运行

uwsgi --http :9090 --wsgi-file server.py

然后打开浏览器,访问”http://localhost:9090″,你就可以看到”Hello World!”字样了。

上面的命令中”- -http”参数指定了HTTP监听地址和端口,”- -wsgi-file”参数指定了WSGI应用程序入口,uWSGI会自动搜寻名为”application”的应用对象并调用它。

更进一步,uWSGI可以支持多进程和多线程的方式启动应用,也可以监控应用的运行状态。我们将启动的命令改为:

uwsgi --http :9090 --wsgi-file server.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191

执行它后,uWSGI将启动4个应用进程,每个进程有2个线程,和一个master主进程(监控其他进程状态,如果有进程死了,则重启)。同时,你可以访问”127.0.0.1:9191″来获取JSON格式的应用运行信息,uWSGI还提供了工具命令”uwsgitop”来像top一样监控应用运行状态,你可以用pip来安装它。


上面的命令参数太多了,我们可以将参数写在配置文件里,启动uWSGI时指定配置文件即可。配置文件可以是键值对的格式,也可以是XML,YAML格式,这里我们使用键值对的格式。让我们创建一个配置文件”myapp.ini”

[uwsgi]

http=:9090

wsgi-file=server.py

master=true

processes=4

threads=2

stats=127.0.0.1:9191

然后就可以将启动命令简化为:

uwsgi myapp.ini


使用uwsgi的时候需要进入python3的虚拟环境,默认是调用python2。

[if !supportLists]2.3 [endif]案例

完成电商里的商品管理模块。


http://ip:port/show 查询所有

http://ip:port/show/1 查询id=1

�����"2

你可能感兴趣的:(day3)