New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure future #411
Ensure future #411
Conversation
I'll add a test and an example @seemethere then you'll see how awesome this is :P |
This simplifies integration with another server like websockets down to this: app = sanic.Sanic()
async def time(websocket, path):
while True:
now = datetime.datetime.utcnow().isoformat() + 'Z'
await websocket.send(now)
await asyncio.sleep(random.random() * 3)
start_server = websockets.serve(time, 'localhost', 3000)
app.ensure_future(start_server)
@app.route('/')
def hello(request):
return html(open('ws.html').read())
app.run(port=8000) Instead of how you would probably do this currently: app = sanic.Sanic()
async def time(websocket, path):
while True:
now = datetime.datetime.utcnow().isoformat() + 'Z'
await websocket.send(now)
await asyncio.sleep(random.random() * 3)
def init(app, loop):
start_server = websockets.serve(time, 'localhost', 3000)
loop.run_until_complete(start_server)
@app.route('/')
def hello(request):
return html(open('ws.html').read())
app.run(port=8000, before_start=init) It works with any Future, coroutine or awaitable, since it's just calling the |
b810f3b
to
ef8a450
Compare
|
||
request, response = sanic_endpoint_test(app, uri='/early') | ||
assert response.body == b'False' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a sleep should be added here just in case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do the sleep in the route, but yes that would also work or both wouldn't hurt. But, if you think about the chain of events, 0.1 in the /late
route should always be enough. Since the first call to sanic_endpoint_test
starts the coro
which only sleeps for 0.05 seconds. Then this route (/late
) sleeps for twice that time, and doesn't start until afterwards. So, I think it's sufficient length...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just dislike those tests that pause for a long time because my heart always sinks thinking I broke something :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're still not convinced I can add it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's okay I think it should be fine.
I'm not sure how this would work exactly with #408 but I'm convinced it's helpful.