简体   繁体   中英

Flask RESTful API structure

I'm attempting to create a simple RESTful style api with flask. I want to send the server some data, have the server run the data through two models I have in parallel before returning an output.

My question is the following. I Would like to pass the data through two models in parallel, so I'm assuming I will need multiprocessing. Additionally, I would not like to load the models each time the api is being called, so I would like to preload the models in advance. Any idea of how to best structure this? My api code snippet sample is below

from flask import Flask, request

app = Flask(__name__)

@app.route('/api', methods = ['POST'])
def api_message():

    if request.headers['Content-Type'] == 'application/octet-stream':
        data = request.data
        #process data in parallel with preloaded models
        return "result"
    else:
        #return error code


if __name__ == '__main__':
    app.run()

Essentially my question boils down to

  1. How can send the data through 2 models in parallel (eg with multiprocessing or whatever)

  2. How can I preload the models in advance so they do not have to be set up each time api_message() is called, as that would be a lot of overhead.

I am new to Flask so any advice is much appreciated, thanks!

OK, it looks like all you need to do load your models is to instantiate them in your flask application. Then, if the module you intend to use to work on your (preloaded) models were called "process_data" and the models were called "Model1" and "Model2", then your code, which I modified slightly and added multiprocessing, would look like this:

from flask import Flask, request
import multiprocessing
import process_data
from models import Model1, Model2

model1 = Model1()
model2 = Model2()

app = Flask(__name__)

models = [model1, model2]

@app.route('/api', methods = ['POST'])
def process_in_parallel():

    if request.headers['Content-Type'] == 'application/octet-stream':
        data = request.data
    else:
        #return error code

    def worker(model, data):
        process_data(model, data)
        return

    jobs = []
    for model in models:
        p = multiprocessing.Process(target=worker(model, data))
        jobs.append(p)
        p.start()

if __name__ == '__main__':
    app.run()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM