'Python' 카테고리의 다른 글
랜덤스트링 만들기 (0) | 2019.03.22 |
---|---|
matplotlib subplot 화면분할 grid로 분할하기 (0) | 2019.03.22 |
WebAPI thread work status (0) | 2018.07.03 |
Python BeautifulSoup 웹크롤링/HTML 파싱. (0) | 2018.04.25 |
Python 데이터 저장/로딩 Pickle (0) | 2018.04.25 |
랜덤스트링 만들기 (0) | 2019.03.22 |
---|---|
matplotlib subplot 화면분할 grid로 분할하기 (0) | 2019.03.22 |
WebAPI thread work status (0) | 2018.07.03 |
Python BeautifulSoup 웹크롤링/HTML 파싱. (0) | 2018.04.25 |
Python 데이터 저장/로딩 Pickle (0) | 2018.04.25 |
[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"
}
matplotlib subplot 화면분할 grid로 분할하기 (0) | 2019.03.22 |
---|---|
File I/O, Directory list, read/write (0) | 2018.07.04 |
Python BeautifulSoup 웹크롤링/HTML 파싱. (0) | 2018.04.25 |
Python 데이터 저장/로딩 Pickle (0) | 2018.04.25 |
Python 커맨드라인 파싱 (0) | 2018.04.17 |
HTML 파싱하여 원하는 부분 추출하기.
File I/O, Directory list, read/write (0) | 2018.07.04 |
---|---|
WebAPI thread work status (0) | 2018.07.03 |
Python 데이터 저장/로딩 Pickle (0) | 2018.04.25 |
Python 커맨드라인 파싱 (0) | 2018.04.17 |
Python 쉘 커맨드 실행 (0) | 2018.04.12 |
파이썬은 자료 구조를 쉽게 파일로 저장 및 로딩할 수 있다.
WebAPI thread work status (0) | 2018.07.03 |
---|---|
Python BeautifulSoup 웹크롤링/HTML 파싱. (0) | 2018.04.25 |
Python 커맨드라인 파싱 (0) | 2018.04.17 |
Python 쉘 커맨드 실행 (0) | 2018.04.12 |
Python JSON 사용하기 (0) | 2018.04.10 |
Python BeautifulSoup 웹크롤링/HTML 파싱. (0) | 2018.04.25 |
---|---|
Python 데이터 저장/로딩 Pickle (0) | 2018.04.25 |
Python 쉘 커맨드 실행 (0) | 2018.04.12 |
Python JSON 사용하기 (0) | 2018.04.10 |
Python 외부 모듈사용.time,string,random, try except (0) | 2018.04.09 |
Python 데이터 저장/로딩 Pickle (0) | 2018.04.25 |
---|---|
Python 커맨드라인 파싱 (0) | 2018.04.17 |
Python JSON 사용하기 (0) | 2018.04.10 |
Python 외부 모듈사용.time,string,random, try except (0) | 2018.04.09 |
Python 강좌7 클래스 (0) | 2018.03.27 |
Python 커맨드라인 파싱 (0) | 2018.04.17 |
---|---|
Python 쉘 커맨드 실행 (0) | 2018.04.12 |
Python 외부 모듈사용.time,string,random, try except (0) | 2018.04.09 |
Python 강좌7 클래스 (0) | 2018.03.27 |
Python 강좌6 tuple, set, dictionary (0) | 2018.03.21 |
Python 쉘 커맨드 실행 (0) | 2018.04.12 |
---|---|
Python JSON 사용하기 (0) | 2018.04.10 |
Python 강좌7 클래스 (0) | 2018.03.27 |
Python 강좌6 tuple, set, dictionary (0) | 2018.03.21 |
Python 강좌5 List (0) | 2018.03.20 |
Python JSON 사용하기 (0) | 2018.04.10 |
---|---|
Python 외부 모듈사용.time,string,random, try except (0) | 2018.04.09 |
Python 강좌6 tuple, set, dictionary (0) | 2018.03.21 |
Python 강좌5 List (0) | 2018.03.20 |
Python 강좌4 정규식 regular expression (0) | 2018.03.19 |
Python 외부 모듈사용.time,string,random, try except (0) | 2018.04.09 |
---|---|
Python 강좌7 클래스 (0) | 2018.03.27 |
Python 강좌5 List (0) | 2018.03.20 |
Python 강좌4 정규식 regular expression (0) | 2018.03.19 |
Python 강좌3 자료형. 수/문자열 (1) | 2018.03.16 |