Skip to content

Instantly share code, notes, and snippets.

@minhoryang
Created June 28, 2019 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save minhoryang/96ecd0ca06cece7c970c5fbf00b26c5e to your computer and use it in GitHub Desktop.
Save minhoryang/96ecd0ca06cece7c970c5fbf00b26c5e to your computer and use it in GitHub Desktop.
[POC] What if asyncio call safely handled within gunicorn-sync-workers, ...

gunicorn -w4 --log-level=DEBUG --timeout=5 app:app

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)
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')
# python >= 3.7
aiohttp-requests
flask
gunicorn
@minhoryang
Copy link
Author

Log:

[2019-06-28 15:37:33 +0900] [92719] [INFO] Starting gunicorn 19.9.0
[2019-06-28 15:37:33 +0900] [92719] [DEBUG] Arbiter booted
[2019-06-28 15:37:33 +0900] [92719] [INFO] Listening at: http://127.0.0.1:8000 (92719)
[2019-06-28 15:37:33 +0900] [92719] [INFO] Using worker: sync
[2019-06-28 15:37:33 +0900] [92772] [INFO] Booting worker with pid: 92772
[2019-06-28 15:37:33 +0900] [92773] [INFO] Booting worker with pid: 92773
[2019-06-28 15:37:33 +0900] [92774] [INFO] Booting worker with pid: 92774
[2019-06-28 15:37:33 +0900] [92775] [INFO] Booting worker with pid: 92775
[2019-06-28 15:37:33 +0900] [92719] [DEBUG] 4 workers
[2019-06-28 15:37:39 +0900] [92775] [DEBUG] GET /
[2019-06-28 15:37:43 +0900] [92773] [DEBUG] GET /call
call 92773 4569712064 0
call 92773 4569712064 1
call 92773 4569712064 2
[2019-06-28 15:37:44 +0900] [92772] [DEBUG] GET /
[2019-06-28 15:37:44 +0900] [92775] [DEBUG] GET /
done 92773 4569712064 1
done 92773 4569712064 2
done 92773 4569712064 0
[2019-06-28 15:37:47 +0900] [92772] [DEBUG] GET /call
call 92772 4569712064 0
call 92772 4569712064 1
call 92772 4569712064 2
[2019-06-28 15:37:48 +0900] [92774] [DEBUG] GET /call
call 92774 4569712064 0
call 92774 4569712064 1
call 92774 4569712064 2
done 92772 4569712064 1
done 92772 4569712064 2
done 92772 4569712064 0
[2019-06-28 15:37:49 +0900] [92775] [DEBUG] GET /call
call 92775 4569712064 0
call 92775 4569712064 1
call 92775 4569712064 2
done 92774 4569712064 1
done 92774 4569712064 2
done 92774 4569712064 0
[2019-06-28 15:37:50 +0900] [92774] [DEBUG] Ignoring EPIPE
done 92775 4569712064 1
done 92775 4569712064 2
done 92775 4569712064 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment