将算法部署成HTTP服务

将算法部署成HTTP服务

2023年7月26日发(作者:)

将算法部署成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 ⼜是啥? 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_ interface:app其中gunicorn_是gunicorn的参数配置⽂件,第⼀个interface是要运⾏模块⾥的代码,第⼆个是指Flask应⽤的名字。下⾯举个简单的例⼦# !/user/bin/env python3# -*- coding:utf-8 -*-# @File: # @Author: Kang# @Time: 2021/08/01 19:00from flask import Flask

app = Flask(__name__)

@('/test')

def test():

return 'Test Finished'

if __name__ == '__main__':

()

下⾯给出⼀个配置⽂件的样例import os# 监听端⼝设置PORT_ENV = ("PORT_ENV", "8080")bind = "0.0.0.0%s"%(PORT_ENV)# 是否以守护进程启动, 默认为falsedaemon = 'false'# 进程/线程/⼯作模式设置workers = 10threads = 3# worker_class = 'gevent'worker_connections = 1000pidfile = 'logs/'# 请求连接相关设置keep_alive = 5timeout = 100000limit_request_line = 8190limit_request_field_size = 0# ⽇志相关设置accesslog = 'logs/gunicorn_'errorlog = 'logs/gunicorn_'loglevel = 'warning'Gunicorn + Flask框架下实现简单异步返回⼀般来说,客户会发送⼀个HTTP请求来获取算法服务。⽐如客户发送⼀张图⽚到服务器,希望服务器识别出这张图⽚是什么东西(算法模型来处理),然后将结果写⼊到数据库。如果不做异步返回处理,要等待计算完成程序才会返回⼀个"Task Finished"信号。现在我们希望程序接收到请求后,将任务放在后台运⾏,并返回⼀个"Task Scheduled"的信号。⼀个简单的⽅法是新开⼀个线程,将主要的计算任务放在该线程进⾏处理。当然,⽹上也有⼀些其它的⽅式可以参考,⽐如说Celery、Gunicorn的Gevent等。from flask import Flask

import threadingapp = Flask(__name__)

def core(arg1, arg2) ...@('/test')

def test():

a1 = 1 a2 = 2 t = (target=core, daemon=None, args=(a1, a2)) () return 'Test Finished'

if __name__ == '__main__':

()

关于()中的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 adaemon 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 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 directlylaunching 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 s 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.

发布者:admin,转转请注明出处:http://www.yc00.com/web/1690377433a340395.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信