-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path协程.py
executable file
·60 lines (43 loc) · 1.34 KB
/
协程.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from time import sleep
from flask import Flask, jsonify, has_request_context, copy_current_request_context
from functools import wraps
from concurrent.futures import Future, ThreadPoolExecutor
import asyncio
def run_async(func):
@wraps(func)
def _wrapper(*args, **kwargs):
call_result = Future()
def _run():
loop = asyncio.new_event_loop()
try:
result = loop.run_until_complete(func(*args, **kwargs))
except Exception as error:
call_result.set_exception(error)
else:
call_result.set_result(result)
finally:
loop.close()
loop_executor = ThreadPoolExecutor(max_workers=10)
if has_request_context():
_run = copy_current_request_context(_run)
loop_future = loop_executor.submit(_run)
loop_future.result()
return call_result.result()
return _wrapper
app = Flask(__name__)
async def slp(n):
sleep(n)
return "r"
@app.route('/async')
@run_async
async def index():
n=10
r = await slp(n)
return jsonify('hello,async')
@app.route('/sync')
def aindex():
sleep(1)
return jsonify('hello,sync')
import logging
# logging.getLogger('werkzeug').setLevel(logging.ERROR)
app.run(port=8000)