gunicorn -w4 --log-level=DEBUG --timeout=5 app:app
Created
June 28, 2019 06:36
-
-
Save minhoryang/96ecd0ca06cece7c970c5fbf00b26c5e to your computer and use it in GitHub Desktop.
[POC] What if asyncio call safely handled within gunicorn-sync-workers, ...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import signal | |
from flask import Flask, jsonify | |
from async_land import call_async_as_sync | |
app = Flask(__name__) | |
@app.route('/') | |
def hello(): | |
return 'okay' | |
@app.route('/call') | |
def calling(): | |
return jsonify(call_async_as_sync()) | |
if __name__ == "__main__": | |
app.serve(debug=True) | |
else: | |
# SIGINFO for debug. | |
def info(*args): | |
import os | |
print(os.getpid(), args) | |
signal.signal(signal.SIGINFO, info) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import os | |
from aiohttp_requests import requests | |
PID = os.getpid() | |
async def _call(idx=0, tid=0): | |
print('call', PID, tid, idx) | |
# XXX: Target Async Func. | |
response = await requests.get('https://httpbin.org/anything') # TODO: more random wait would be dramatic. | |
json = await response.json() | |
print('done', PID, tid, idx) | |
return json | |
async def _multiple_calls(loop): | |
return await asyncio.gather( | |
_call(0, loop._thread_id), | |
_call(1, loop._thread_id), | |
_call(2, loop._thread_id), | |
) | |
def call_async_as_sync(): | |
loop = asyncio.get_event_loop() | |
ret = loop.run_until_complete( | |
_multiple_calls(loop) # FIXME: timeout? | |
) | |
# XXX: no need to close the loop | |
# print(loop.is_running()) | |
# loop.close() | |
return ret | |
if __name__ == "__main__": | |
print('s') | |
print(call_async_as_sync()) | |
print('e') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python >= 3.7 | |
aiohttp-requests | |
flask | |
gunicorn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Log: