SoFunction
Updated on 2024-11-17

python http service flask architecture utility code detailed analysis

dependency library (computing)

flask installation, accelerated using bean source.

pip install flask -i /simple

gevent installation, accelerated using the bean source.

pip install gevent -i /simple

coding

#!/user/bin/env python
# coding=utf-8
"""
@project : TestDemo
@author  : huyi
@file   : 
@ide    : PyCharm
@time   : 2021-10-15 11:58:44
"""
from flask import Flask, request
from  import WSGIServer
from gevent import monkey
import json
 
# Replace python's standard io methods with methods of the same name in gevent, and when io blocks gevent automatically switches to a concatenation.
monkey.patch_all()
 
app = Flask(__name__)
 
 
# Request return entity
class TestResponse:
    def __init__(
            self,
            code,
            success,
            msg, data):
         = code
         = success
         = msg
         = data
 
 
@('/progress', methods=['POST'])
def progress():
    request_data = ()
    print("progress callback:{}".format(request_data))
    return (
        TestResponse(0, True, "progress callback success", None),
        default=lambda obj: obj.__dict__,
        sort_keys=True,
        indent=4)
 
 
@('/result', methods=['POST'])
def result():
    request_data = ()
    print("Results callbacks:{}".format(request_data))
    return (
        TestResponse(0, True, "result callback success", None),
        default=lambda obj: obj.__dict__,
        sort_keys=True,
        indent=4)
 
 
if __name__ == '__main__':
    print("When in doubt, ask the spring breeze.")
    WSGIServer(('0.0.0.0', 8383), app).serve_forever()

Validating results with postman

summarize

The code architecture is simple and effective, without adding too much heavy stuff. If you need asynchronous non-blocking interface calls, you can add a thread pool and throw a thread out of the execution, which is just as applicable.

If this article was helpful to you, please like and support it.

to this article on the python http service flask architecture practical code detailed analysis of the article is introduced to this, more related python http service content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!