多个进程启动flask服务

需求:

  在不同的进程中分别启动flask服务,或将其部署到不同的端口号上。

# 导入flask类
import flask
import multiprocessing as mp
import time

# 创建Flask实例,接收参数__name__表示当前文件名,默认会自动指定静态文件static
app1 = flask.Flask(__name__)
app2 = flask.Flask(__name__)
app3 = flask.Flask(__name__)

# app1
@app1.route('/',methods=['POST', 'GET'])
def hello_word1():
    time.sleep(1)
    return '<h1>Hello World!</h1>'

# app2
@app2.route('/',methods=['POST', 'GET'])
def hello_word2():
    return '<h1>Hello Fancp!</h1>'

# app3
@app3.route('/',methods=['POST', 'GET'])
def hello_word3():
    return '<h1>Hello nihao!</h1>'


def startServer1(inDebug, port1): 
    print("Starting Main Mock Server1") 
    app1.run(debug=inDebug, port=port1) 


def startServer2(inDebug, port2): 
    print("Starting Main Mock Server2")
    app2.run(debug=inDebug, port=port2) 

#参考链接:http://cn.voidcc.com/question/p-npbaicvr-bdp.html
if __name__ == '__main__': 
    p = mp.Process(target=startServer1, args=(False, 8082)) 
    p.deamon = True 
    p.start() 
    p1 = mp.Process(target=startServer2, args=(False, 8083)) 
    p1.deamon = True 
    p1.start() 
    p.join() 
    p1.join()

输出结果:

Starting Main Mock Server1
Starting Main Mock Server2
 * Serving Flask app "__mp_main__" (lazy loading)
 * Serving Flask app "__mp_main__" (lazy loading)
 * Environment: production
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.   WARNING: This is a development server. Do not use it in a production deployment.

   Use a production WSGI server instead.   Use a production WSGI server instead.
 * Debug mode: off

 * Debug mode: off
 * Running on http://127.0.0.1:8082/ (Press CTRL+C to quit)
 * Running on http://127.0.0.1:8083/ (Press CTRL+C to quit)
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peanut_范

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值