반응형

[Python에서 API서비스 구현하기]


POST로 JSON 요청받아 작업쓰레드를 돌려서 비동기 방식으로 작업하고,

진행 상태를 체크할 수 있는 구조.

Flask 사용.



- 구동 쉘 스크립트 ; server_run.sh

#!/bin/bash
/opt/anaconda3/envs/tensorflow/bin/python server_learn.py

- 서버 ; server_learn.py


from flask import Flask, request
from flask_restful import Resource, Api
from flask.views import MethodView
import json, base64
import string
import random
import os
import threading, time

app = Flask(__name__)
api = Api(app)

# 랜덤스트링 생성하는 함수. 세션키 생성 등
def random_string(size=6, chars=string.ascii_lowercase+string.digits):
return ''.join(random.choice(chars) for _ in range(size))


# 글로벌
g_bworking=False    # 작업 여부.
g_progress = 0        # 진행 상태. 0~100
g_elapsedtime = 0    # 소요 시간. sec
g_workresult={}    # 작업 결과
g_workname=''       # 작업명

# 워커 쓰레드 ; 1~p1까지 sum구하기. 루프 돌때마다 p2 sec만큼 sleep.
def work(myid, p1, p2):
global g_bworking, g_progress, g_workresult, g_elapsedtime
starttime = time.time()

if g_bworking==True:
print('server is busy...')
return 0
g_bworking=True
g_workresult={}
g_progress=0
g_elapsedtime=0
total = 0
for i in range(1, (p1+1)):
total+=i
time.sleep(p2)
print('work : total[',myid,'] = ', total)

g_progress = 100* i / (p1)
g_elapsedtime = time.time() - starttime


endtime = time.time()
g_elapsedtime = endtime - starttime
g_workresult={'total':total, 'until':p1, 'sleep': p2}
g_progress=100
g_bworking=False
return total


class mysum(MethodView):
def get(self):
data = request.args
print(data) # dictionary
return {'name':'test'}

def options(self):
print('options.')
data = request.get_json() # application/json
print(data)
return {'Allow':'PUT,GET,POST'}, 200, \
{'Access-Control-Allow-Origin': '*', \
'Access-Control-Allow-Headers':'Content-Type,Authorization',\
'Access-Control-Allow-Methods': 'PUT,GET,POST'}

def post(self):
global g_workname
# data = request.form
# print(data) # dictionary
data = request.get_json() # application/json
print(data)

if g_bworking==True:
LearnResult = { 'result': 0 }
else:
num = int(data['num'])
slp = int(data['slp'])
g_workname = data['name']
t = threading.Thread(target=work, args=(g_workname, num, slp))
t.start()
LearnResult = {
'result': 1
}
return LearnResult


class status(MethodView):
def get(self):
data = request.args
print(data) # dictionary
if g_progress > 0 :
predtime = g_elapsedtime * 100 / g_progress
else:
predtime = 0
result = { 'work':int(g_bworking),
'workname':g_workname,
'progress':str(g_progress),
'workresult': g_workresult,
'elapsedtime':str(g_elapsedtime),
'remaintime': str(predtime - g_elapsedtime) }
return result

api.add_resource(mysum, '/mysum')
api.add_resource(status, '/status')

if __name__=='__main__':
app.run(host='0.0.0.0', port=18899, debug=True)


- 테스트

http://127.0.0.1:18899/mysum

POSTDATA ; 1~10까지 합 요청. 단계별로 1초씩 쉼.

{

    "num": "10",

    "slp": "1",

    "name": "work1"

}

response: 즉시 아래와 같이 성공 리턴됨.

{

    "result": 1

}



http://127.0.0.1:18899/status

GET

주기적으로 상태 체크 API 호출하여 결과 확인.

{

    "workresult": {

        "until": 10,

        "total": 55,

        "sleep": 1

    },

    "remaintime": "0.0",

    "workname": "work1",

    "elapsedtime": "10.011629581451416",

    "work": 0,

    "progress": "100"

}









+ Recent posts