Merge pull request #238 from r0fls/73

stop multiple worker server without sleep loop; issue #73
This commit is contained in:
Eli Uriegas 2016-12-28 18:05:06 -06:00 committed by GitHub
commit 7dfa62516c
2 changed files with 27 additions and 4 deletions

View File

@ -3,8 +3,8 @@ from collections import deque
from functools import partial
from inspect import isawaitable, stack, getmodulename
from multiprocessing import Process, Event
from select import select
from signal import signal, SIGTERM, SIGINT
from time import sleep
from traceback import format_exc
import logging
@ -352,8 +352,7 @@ class Sanic:
# Infinitely wait for the stop event
try:
while not stop_event.is_set():
sleep(0.3)
select(stop_event)
except:
pass

View File

@ -1,5 +1,5 @@
from multiprocessing import Array, Event, Process
from time import sleep
from time import sleep, time
from ujson import loads as json_loads
from sanic import Sanic
@ -51,3 +51,27 @@ def skip_test_multiprocessing():
raise ValueError("Expected JSON response but got '{}'".format(response))
assert results.get('test') == True
def test_drain_connections():
app = Sanic('test_json')
@app.route('/')
async def handler(request):
return json({"test": True})
stop_event = Event()
async def after_start(*args, **kwargs):
http_response = await local_request('get', '/')
stop_event.set()
start = time()
app.serve_multiple({
'host': HOST,
'port': PORT,
'after_start': after_start,
'request_handler': app.handle_request,
}, workers=2, stop_event=stop_event)
end = time()
assert end - start < 0.05