Skip to content
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

Merged
merged 3 commits into from Feb 13, 2017
Merged

Ensure future #411

merged 3 commits into from Feb 13, 2017

Conversation

r0fls
Copy link
Contributor

@r0fls r0fls commented Feb 12, 2017

I'm not sure how this would work exactly with #408 but I'm convinced it's helpful.

@r0fls
Copy link
Contributor Author

r0fls commented Feb 12, 2017

I'll add a test and an example @seemethere then you'll see how awesome this is :P

@r0fls
Copy link
Contributor Author

r0fls commented Feb 12, 2017

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 ensure_future method later.

@r0fls r0fls force-pushed the ensure-future branch 2 times, most recently from b810f3b to ef8a450 Compare February 12, 2017 20:23

request, response = sanic_endpoint_test(app, uri='/early')
assert response.body == b'False'

Copy link
Member

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?

Copy link
Contributor Author

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...

Copy link
Contributor Author

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 :)

Copy link
Contributor Author

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 :)

Copy link
Member

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.

@seemethere seemethere added this to the 0.4.0 milestone Feb 13, 2017
@seemethere seemethere merged commit a6a07c3 into sanic-org:master Feb 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants