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

No asyncio global settings #385

Closed

Conversation

seemethere
Copy link
Member

Overview

This pull request aims to fix issues like #381, #305, #275, and #152. It adds an unambiguous way to get the loop from sanic and works to remove the need to set global asyncio settings as identified by @JendaPlhak here. Which in my opinion third party libraries, like sanic, should not do.

Example:

from sanic.policy import get_event_loop

def thing_that_needs_event_loop():
    loop = get_event_loop()

Implementation

Basic implementation was done by storing an instantiated uvloop policy inside of sanic.policy and then using that to set the global loop for the sanic server at that time.

What this means going forward

  1. No more passing the loop
  • Server events before/after_start will be the preferred way of sharing the loop but you can also get it through sanic.policy.get_event_loop
  1. There is not going to be a way to have a non-blocking way to start a Sanic application
  • There is no way to get that functionality to work with multiprocessing
  • The use cases could be fulfilled in different ways (like running separate apps that talk to each other through an API)

Also moves sanic.py -> app.py and removes the need for us to relative
import anymore.

This is a step towards allowing people to use things like
`sanic.get_event_loop()` instead of having to set global asyncio
settings.

Does not seem to work on 3.5.1 but works fine on every version after
that.

_policy = uvloop.EventLoopPolicy()

def get_event_loop():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to add this as a property of the sanic app itself. E.g., have sanic.loop return the results of get_event_loop(). What do you think? Also, does this get_event_loop always return the current, running event loop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could possibly be good, maybe each app has it's own instantiated event policy?

@seemethere
Copy link
Member Author

This can be revisited later

@seemethere seemethere closed this Feb 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants