您好,欢迎来到赴品旅游。
搜索
您的当前位置:首页将算法部署成HTTP服务

将算法部署成HTTP服务

来源:赴品旅游
将算法部署成HTTP服务

算法⼯程师有时需要将模型部署在服务器上,然后根据定义好的接⼝向外部提供⼀个HTTP服务,使⽤户能够调⽤这⼀算法。下⾯记录⼀下使⽤Flask + Gunicorn的⽅案,以及其中涉及的⼀些知识点。

Gunicorn和Flask简介

Gunicorn是个啥,根据其官⽹定义:

⾸先它是⼀个遵循WSGI协议的HTTP服务器。

Gunicorn 'Green Unicorn' is a Python WSGI HTTP Server for UNIX. It's a pre-fork worker model. The Gunicorn server is broadlycompatible with various web frameworks, simply implemented, light on server resources, and fairly speedy.

WSGI⼜是啥? WSGI是Web Server Gateway Interface的缩写,翻译过来就是Web服务器⽹关接⼝。根据WIKI上的描述,它是Web服务器将请求转发到以Python语⾔编写的Web应⽤程序或框架的简单调⽤约定。像Nginx、Apache HTTP Server都是常见的Web服务器软件,⽽像Django、Pyramid、Flask、Web2py则是所谓的Web框架。

The Web Server Gateway Interface is a simple calling convention for web servers to forward requests to web applications orframeworks written in the Python programming language. The current version of WSGI, version 1.0.1, is specified in PythonEnhancement Proposal (PEP) 3333.[4] WSGI was originally specified as PEP-333 in 2003.[5] PEP-3333, published in 2010,updates the specification for Python 3.

这⾥强烈推荐知乎⽂章去了解上⽂所提及到的⼀些内容。这篇⽂章最后提到,虽然Flask是Web框架,但是它⾃带了服务器,⽽且这是⼀个WSGI服务器,只不过性能低,不⽀持⾼并发,只能⽤于测试。

其次它是⼀个pre-fork worker模型

关于这点可以参考⽂章。所谓pre-fork就是在request到来之前,先fork出多个worker⼦进程⽤来负责处理请求。

Gunicorn使⽤⽅法

在Linux下安装好gunicorn后,通过下⾯的命令⾏就可以启动应⽤程序了。

gunicorn -c gunicorn_conf.py interface:app

其中gunicorn_conf.py是gunicorn的参数配置⽂件,第⼀个interface是要运⾏interface.py模块⾥的代码,第⼆个是指Flask应⽤的名字。下⾯举个简单的例⼦

# !/user/bin/env python3# -*- coding:utf-8 -*-# @File: interface.py# @Author: Kang

# @Time: 2021/08/01 19:00from flask import Flask app = Flask(__name__) @app.route('/test') def test():

return 'Test Finished' if __name__ == '__main__': app.run()

下⾯给出⼀个配置⽂件的样例

import os

# 监听端⼝设置

PORT_ENV = os.getenv(\"PORT_ENV\bind = \"0.0.0.0%s\"%(PORT_ENV)# 是否以守护进程启动, 默认为falsedaemon = 'false'

# 进程/线程/⼯作模式设置workers = 10threads = 3

# worker_class = 'gevent'worker_connections = 1000pidfile = 'logs/gunicorn.pid'# 请求连接相关设置keep_alive = 5timeout = 100000

limit_request_line = 8190limit_request_field_size = 0# ⽇志相关设置

accesslog = 'logs/gunicorn_access.log'errorlog = 'logs/gunicorn_error.log'loglevel = 'warning'

Gunicorn + Flask框架下实现简单异步返回

⼀般来说,客户会发送⼀个HTTP请求来获取算法服务。⽐如客户发送⼀张图⽚到服务器,希望服务器识别出这张图⽚是什么东西(算法模型来处理),然后将结果写⼊到数据库。如果不做异步返回处理,要等待计算完成程序才会返回⼀个\"Task Finished\"信号。现在我们希望程序接收到请求后,将任务放在后台运⾏,并返回⼀个\"Task Scheduled\"的信号。⼀个简单的⽅法是新开⼀个线程,将主要的计算任务放在该线程进⾏处理。当然,⽹上也有⼀些其它的⽅式可以参考,⽐如说Celery、Gunicorn的Gevent等。

from flask import Flask import threading

app = Flask(__name__) def core(arg1, arg2) ...

@app.route('/test') def test(): a1 = 1 a2 = 2

t = threading.Thread(target=core, daemon=None, args=(a1, a2)) t.start()

return 'Test Finished' if __name__ == '__main__': app.run()

关于threading.Thread()中的daemon参数。如果⼀个thread被设置成daemon=True,那么当main program退出时(或者说当所有non-daemonthread都结束退出时), daemon参数为True的thread会⾃动被kill掉。如果daemon不设置为True,那么即便主程序(其中⼀个thread)退出了,我们另外那个线程还会继续把它的任务做完。Daemon thread通常⽤在那些⼀直循环、⾃⼰不知道退出的情况下。参考

但是,这⾥的daemon参数和我们常说的daemon process有些细微差别。守护进程是⼀种脱离了终端、在后台运⾏的特殊进程,这使它能够避免被任何终端产⽣的信号打断。WIKI中的解释为

In multitasking computer operating systems, a daemon (/ˈdiːmən/ or /ˈdeɪmən/)[1] is a computer program that runs as abackground process, rather than being under the direct control of an interactive user. Traditionally, the process names of a

daemon end with the letter d, for clarification that the process is in fact a daemon, and for differentiation between a daemon anda normal computer program. For example, syslogd is a daemon that implements system logging facility, and sshd is a daemonthat serves incoming SSH connections.

In a Unix environment, the parent process of a daemon is often, but not always, the init process(PID=1). A daemon is usuallycreated either by a process forking a child process and then immediately exiting, thus causing init to adopt the child process(⼀个⽗进程已经terminated的进程称为孤⼉进程orphan process, 孤⼉进程将由init进程收养), or by the init process directly

launching the daemon. In addition, a daemon launched by forking and exiting typically must perform other operations, such asdissociating the process from any controlling terminal (tty). Such procedures are often implemented in various convenienceroutines such as daemon(3) in Unix.

Systems often start daemons at boot time that will respond to network requests, hardware activity, or other programs byperforming some task. Daemons such as cron may also perform defined tasks at scheduled times.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- fupindai.com 版权所有 赣ICP备2024042792号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务