Browse Source

flask 비동기 도입

잉여개발기 (SPDV) 2 năm trước cách đây
mục cha
commit
08e5ee02f1

+ 9 - 8
app.py

@@ -6,6 +6,9 @@ import logging
 from route.tool.func import *
 from route import *
 
+from hypercorn.asyncio import serve
+from hypercorn.config import Config
+
 # Init-Version
 with open('version.json', encoding = 'utf8') as file_data:
     version_list = json.loads(file_data.read())
@@ -143,7 +146,7 @@ with get_db_connect() as conn:
     app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
     app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600
 
-    log = logging.getLogger('waitress')
+    log = logging.getLogger('hypercorn')
     log.setLevel(logging.ERROR)
 
     app.jinja_env.filters['md5_replace'] = md5_replace
@@ -805,10 +808,8 @@ app.route('/update', methods = ['POST', 'GET'])(main_sys_update)
 app.errorhandler(404)(main_func_error_404)
 
 if __name__ == "__main__":
-    waitress.serve(
-        app,
-        host = server_set['host'],
-        port = int(server_set['port']),
-        clear_untrusted_proxy_headers = True,
-        threads = os.cpu_count()
-    )
+    config = Config()
+    config.bind = [f"{server_set['host']}:{server_set['port']}"]
+    
+    # hypercorn 서버 실행
+    asyncio.run(serve(app, config))

+ 2 - 1
requirements.txt

@@ -1,7 +1,8 @@
 pip
 
 flask
-waitress
+flask[async]
+hypercorn
 
 requests
 diff-match-patch

+ 2 - 2
route/go_api_bbs.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
-def api_bbs(bbs_num = "", page = 1):
+async def api_bbs(bbs_num = "", page = 1):
     other_set = {}
     other_set["bbs_num"] = str(bbs_num)
     other_set["page"] = str(page)
     other_set["ip"] = ip_check()
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_bbs_list.py

@@ -1,4 +1,4 @@
 from .tool.func import *
 
-def api_bbs_list():
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name), status = 200, mimetype = 'application/json')
+async def api_bbs_list():
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_bbs_w_comment.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
-def api_bbs_w_comment_n(bbs_num = "", post_num = "", tool = "length"):
+async def api_bbs_w_comment_n(bbs_num = "", post_num = "", tool = "length"):
     other_set = {}
     other_set["bbs_num"] = str(bbs_num)
     other_set["post_num"] = str(post_num)
     other_set["tool"] = tool
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_func_auth_list.py

@@ -1,7 +1,7 @@
 from .tool.func import *
 
-def api_func_auth_list(user_name = ''):
+async def api_func_auth_list(user_name = ''):
     other_set = {}
     other_set["ip"] = ip_check() if user_name == '' else user_name
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_func_ip.py

@@ -1,8 +1,8 @@
 from .tool.func import *
 
-def api_func_ip(data = 'Test'):
+async def api_func_ip(data = 'Test'):
     other_set = {}
     other_set["data"] = data
     other_set["ip"] = ip_check()
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_func_ip_menu.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
-def api_func_ip_menu(ip = "Test", option = ""):
+async def api_func_ip_menu(ip = "Test", option = ""):
     other_set = {}
     other_set["ip"] = ip
     other_set["my_ip"] = ip_check()
     other_set["option"] = option
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_func_language.py

@@ -1,6 +1,6 @@
 from .tool.func import *
 
-def api_func_language(data = 'Test'):
+async def api_func_language(data = 'Test'):
     other_set = {}
     if flask.request.method == 'POST':
         other_set["data"] = flask.request.form.get('data', '')
@@ -8,4 +8,4 @@ def api_func_language(data = 'Test'):
     else:
         other_set["data"] = [data]
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_func_llm.py

@@ -1,11 +1,11 @@
 from .tool.func import *
 
-def api_func_llm():
+async def api_func_llm():
     if flask.request.method == 'POST':
         other_set = {}
         other_set["prompt"] = flask.request.form.get('prompt', '')
         other_set["ip"] = ip_check()
 
-        return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+        return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
     else:
         return flask.jsonify({})

+ 2 - 2
route/go_api_func_sha224.py

@@ -1,7 +1,7 @@
 from .tool.func import *
 
-def api_func_sha224(data = 'Test'):
+async def api_func_sha224(data = 'Test'):
     other_set = {}
     other_set["data"] = data
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_list_old_page.py

@@ -1,8 +1,8 @@
 from .tool.func import *
 
-def api_list_old_page(num = 1, set_type = 'old'):
+async def api_list_old_page(num = 1, set_type = 'old'):
     other_set = {}
     other_set["num"] = str(num)
     other_set["set_type"] = set_type
     
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 3 - 3
route/go_api_list_recent_block.py

@@ -1,10 +1,10 @@
 from .tool.func import *
 
-def api_list_recent_block(num = 1, set_type = 'all', user_name = 'Test'):
+async def api_list_recent_block(num = 1, set_type = 'all', user_name = 'Test'):
     other_set = {}
     other_set["num"] = str(num)
     other_set["set_type"] = set_type
     other_set["user_name"] = user_name
     other_set["ip"] = ip_check()
-    
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_list_recent_change.py

@@ -1,6 +1,6 @@
 from .tool.func import *
 
-def api_list_recent_change(num = 1, set_type = 'normal', limit = 10, legacy = 'on'):
+async def api_list_recent_change(num = 1, set_type = 'normal', limit = 10, legacy = 'on'):
     other_set = {}
     other_set["num"] = str(num)
     other_set["limit"] = str(limit)
@@ -8,7 +8,7 @@ def api_list_recent_change(num = 1, set_type = 'normal', limit = 10, legacy = 'o
     other_set["legacy"] = legacy
     other_set["ip"] = ip_check()
 
-    response = flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    response = flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
     
     response.headers.add("Access-Control-Allow-Origin", "*")
     response.headers.add('Access-Control-Allow-Headers', "Content-Type")

+ 2 - 2
route/go_api_list_recent_discuss.py

@@ -1,6 +1,6 @@
 from .tool.func import *
 
-def api_list_recent_discuss(num = 1, set_type = 'normal', limit = 10, legacy = 'on'):
+async def api_list_recent_discuss(num = 1, set_type = 'normal', limit = 10, legacy = 'on'):
     other_set = {}
     other_set["num"] = str(num)
     other_set["limit"] = str(limit)
@@ -8,7 +8,7 @@ def api_list_recent_discuss(num = 1, set_type = 'normal', limit = 10, legacy = '
     other_set["legacy"] = legacy
     other_set["ip"] = ip_check()
 
-    response = flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    response = flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
     
     response.headers.add("Access-Control-Allow-Origin", "*")
     response.headers.add('Access-Control-Allow-Headers', "Content-Type")

+ 2 - 2
route/go_api_list_recent_edit_request.py

@@ -1,10 +1,10 @@
 from .tool.func import *
 
-def api_list_recent_edit_request(num = 1, set_type = 'normal', limit = 50):
+async def api_list_recent_edit_request(num = 1, set_type = 'normal', limit = 50):
     other_set = {}
     other_set["num"] = str(num)
     other_set["limit"] = str(limit)
     other_set["set_type"] = set_type
     other_set["ip"] = ip_check()
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_list_title_index.py

@@ -1,7 +1,7 @@
 from .tool.func import *
 
-def api_list_title_index(num = 1):
+async def api_list_title_index(num = 1):
     other_set = {}
     other_set["num"] = str(num)
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_search.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
-def api_search(name = 'Test', search_type = 'title', num = 1):
+async def api_search(name = 'Test', search_type = 'title', num = 1):
     other_set = {}
     other_set["name"] = name
     other_set["search_type"] = search_type
     other_set["num"] = str(num)
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_setting.py

@@ -1,6 +1,6 @@
 from .tool.func import *
 
-def api_setting(name = 'Test'):
+async def api_setting(name = 'Test'):
     other_set = {}
     other_set["set_name"] = name
     other_set["ip"] = ip_check()
@@ -10,4 +10,4 @@ def api_setting(name = 'Test'):
         func_name += '_edit'
         other_set['data'] = flask.request.form.get('data', 'Test')
     
-    return flask.Response(response = python_to_golang(func_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(func_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_topic.py

@@ -120,7 +120,7 @@ def api_topic_thread_pre_render(conn, data, num, ip, topic_num = '', name = '',
 
     return data
 
-def api_topic(topic_num = 1, tool = 'normal', s_num = '', e_num = ''):
+async def api_topic(topic_num = 1, tool = 'normal', s_num = '', e_num = ''):
     with get_db_connect() as conn:
         topic_num = str(topic_num)
 
@@ -132,6 +132,6 @@ def api_topic(topic_num = 1, tool = 'normal', s_num = '', e_num = ''):
             other_set["e_num"] = str(e_num)
             other_set["ip"] = ip_check()
 
-            return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+            return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
         else:
             return flask.jsonify({})

+ 2 - 2
route/go_api_topic_list.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
-def api_topic_list(name = 'Test', set_type = 'normal', num = 1):
+async def api_topic_list(name = 'Test', set_type = 'normal', num = 1):
     other_set = {}
     other_set["name"] = str(name)
     other_set["set_type"] = set_type
     other_set["num"] = str(num)
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_user_setting_editor.py

@@ -1,6 +1,6 @@
 from .tool.func import *
 
-def api_user_setting_editor():
+async def api_user_setting_editor():
     other_set = {}
     other_set["ip"] = ip_check()
     
@@ -12,4 +12,4 @@ def api_user_setting_editor():
         func_name += '_delete'
         other_set['data'] = flask.request.form.get('data', 'Test')
 
-    return flask.Response(response = python_to_golang(func_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(func_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_w_random.py

@@ -1,4 +1,4 @@
 from .tool.func import *
 
-def api_w_random():
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name), status = 200, mimetype = 'application/json')
+async def api_w_random():
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_w_raw.py

@@ -1,6 +1,6 @@
 from .tool.func import *
 
-def api_w_raw(name = 'Test', rev = '', exist_check = ''):
+async def api_w_raw(name = 'Test', rev = '', exist_check = ''):
     with get_db_connect() as conn:
         if acl_check(conn, name, 'render') != 1:
             other_set = {}
@@ -8,6 +8,6 @@ def api_w_raw(name = 'Test', rev = '', exist_check = ''):
             other_set["rev"] = str(rev)
             other_set["exist_check"] = exist_check
 
-            return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+            return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
         else:
             return flask.jsonify({})

+ 3 - 3
route/go_api_w_render.py

@@ -26,7 +26,7 @@ class api_w_render_include:
             else:
                 return slash_add + match[2]
 
-def api_w_render(name = '', tool = ''):
+async def api_w_render(name = '', tool = ''):
     with get_db_connect() as conn:
         curs = conn.cursor()
 
@@ -87,7 +87,7 @@ def api_w_render(name = '', tool = ''):
                 other_set["doc_name"] = name
                 other_set["render_type"] = data_type
                 other_set["data"] = data_org
-                
-                return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+
+                return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
         else:
             return flask.jsonify({})

+ 2 - 2
route/go_api_w_set_reset.py

@@ -1,8 +1,8 @@
 from .tool.func import *
 
-def api_w_set_reset(name = 'Test'):
+async def api_w_set_reset(name = 'Test'):
     other_set = {}
     other_set["name"] = name
     other_set["ip"] = ip_check()
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_w_watch_list.py

@@ -1,10 +1,10 @@
 from .tool.func import *
 
-def api_w_watch_list(name = 'Test', do_type = 'watch_list', num = 1):
+async def api_w_watch_list(name = 'Test', do_type = 'watch_list', num = 1):
     other_set = {}
     other_set["name"] = name
     other_set["do_type"] = do_type
     other_set["ip"] = ip_check()
     other_set["num"] = str(num)
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 2 - 2
route/go_api_w_xref.py

@@ -1,9 +1,9 @@
 from .tool.func import *
 
-def api_w_xref(name = 'Test', page = 1, xref_type = '1'):
+async def api_w_xref(name = 'Test', page = 1, xref_type = '1'):
     other_set = {}
     other_set["name"] = name
     other_set["page"] = str(page)
     other_set["do_type"] = xref_type
 
-    return flask.Response(response = python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')
+    return flask.Response(response = await python_to_golang(sys._getframe().f_code.co_name, other_set), status = 200, mimetype = 'application/json')

+ 14 - 8
route/tool/func.py

@@ -75,12 +75,11 @@ from .func_render import class_do_render
 
 from diff_match_patch import diff_match_patch
 
-import waitress
-
 import werkzeug.routing
 import werkzeug.debug
 
 import flask
+import asyncio
 
 import requests
 from PIL import Image
@@ -114,7 +113,7 @@ def do_db_set(db_set):
         for for_a in db_set:
             m_curs.execute('insert into temp (name, data) values (?, ?)', ['db_' + for_a, db_set[for_a]])
 
-def python_to_golang(func_name, other_set = {}):
+async def python_to_golang(func_name, other_set = {}):
     if other_set == {}:
         other_set = '{}'
     else:
@@ -122,16 +121,23 @@ def python_to_golang(func_name, other_set = {}):
 
     if platform.system() == 'Linux':
         if platform.machine() in ["AMD64", "x86_64"]:
-            data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.bin"), func_name, other_set], stdout = subprocess.PIPE).communicate()[0]
+            cmd = [os.path.join(".", "route_go", "bin", "main.amd64.bin"), func_name, other_set]
         else:
-            data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.bin"), func_name, other_set], stdout = subprocess.PIPE).communicate()[0]
+            cmd = [os.path.join(".", "route_go", "bin", "main.arm64.bin"), func_name, other_set]
     else:
         if platform.machine() in ["AMD64", "x86_64"]:
-            data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.amd64.exe"), func_name, other_set], stdout = subprocess.PIPE).communicate()[0]
+            cmd = [os.path.join(".", "route_go", "bin", "main.amd64.exe"), func_name, other_set]
         else:
-            data = subprocess.Popen([os.path.join(".", "route_go", "bin", "main.arm64.exe"), func_name, other_set], stdout = subprocess.PIPE).communicate()[0]
+            cmd = [os.path.join(".", "route_go", "bin", "main.arm64.exe"), func_name, other_set]
+
+    process = await asyncio.create_subprocess_exec(
+        *cmd,
+        stdout = asyncio.subprocess.PIPE,
+        stderr = asyncio.subprocess.PIPE
+    )
 
-    data = data.decode('utf8')
+    stdout, stderr = await process.communicate()
+    data = stdout.decode('utf8')
 
     return data