-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
make Sanic.create_server return an asyncio.Server #1470
Conversation
- adding 2 new parameters to Sanic.create_server: * return_asyncio_server=False - defines whether there's a need to return an asyncio.Server or run it right away * asyncio_server_kwargs=None - for python 3.7 uvloop doesn't support all necessary features like "start_serving", so, in order to make sanic work well with asyncio from 3.7 there's a need to introduce generic way for passing kwargs for "loop.create_server" Closes: sanic-org#1469
This looks promising, I really like it. One thing, though: it should be awesome to see some more tests regarding specific PS: the current tests are not passing on travis, if you can take a look, it would be awesome 😉 |
61d4a50
to
eed22a7
Compare
Codecov Report
@@ Coverage Diff @@
## master #1470 +/- ##
=======================================
Coverage 91.45% 91.45%
=======================================
Files 17 17
Lines 1732 1732
Branches 329 329
=======================================
Hits 1584 1584
Misses 123 123
Partials 25 25
Continue to review full report at Codecov.
|
@vltr would you mind to review this PR, please? |
With Python 3.7 AsyncIO got major update for the following types: | ||
|
||
- asyncio.AbstractEventLoop | ||
- asyncio.AnstractServer |
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.
typo: AbstractServer
Hey @denismakogon I'm trying to understand this change. I understand your original intention in #1469, but there's a few adaptations here. specifically, could you give an example using $ cat test.py
import asyncio
import sanic
app = sanic.Sanic()
async def main():
server = await app.create_server()
print(server)
print(type(server))
asyncio.run(main())
$ python3 -m venv venv18.12
$ venv18.12/bin/pip install sanic==18.12.0
...
Successfully installed aiofiles-0.4.0 httptools-0.0.13 multidict-4.5.2 sanic-18.12.0 ujson-1.35 uvloop-0.12.2 websockets-6.0
$ python3 -m venv venv19.3
$ venv19.3/bin/pip install sanic==19.3.1
...
Successfully installed aiofiles-0.4.0 httptools-0.0.13 multidict-4.5.2 sanic-19.3.1 ujson-1.35 uvloop-0.12.2 websockets-6.0
$ venv18.12/bin/python test.py
[2019-03-27 11:42:21 +0000] [34824] [INFO] Goin' Fast @ http://127.0.0.1:8000
<Server sockets=[<uvloop.PseudoSocket fd=13, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8000)>]>
<class 'uvloop.loop.Server'>
$ venv19.3/bin/python test.py
[2019-03-27 11:49:53 +0000] [35015] [INFO] Goin' Fast @ http://127.0.0.1:8000
[2019-03-27 11:49:53 +0000] [35015] [ERROR] Unable to start server
Traceback (most recent call last):
File "/private/tmp/venv19.3/lib/python3.7/site-packages/sanic/server.py", line 745, in serve
http_server = loop.run_until_complete(server_coroutine)
File "uvloop/loop.pyx", line 1445, in uvloop.loop.Loop.run_until_complete
File "uvloop/loop.pyx", line 1438, in uvloop.loop.Loop.run_until_complete
File "uvloop/loop.pyx", line 1347, in uvloop.loop.Loop.run_forever
File "uvloop/loop.pyx", line 452, in uvloop.loop.Loop._run
RuntimeError: Cannot run the event loop while another loop is running
Traceback (most recent call last):
File "test.py", line 11, in <module>
asyncio.run(main())
File "/usr/local/Cellar/python/3.7.2_2/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "uvloop/loop.pyx", line 1451, in uvloop.loop.Loop.run_until_complete
File "test.py", line 7, in main
server = await app.create_server()
File "/private/tmp/venv19.3/lib/python3.7/site-packages/sanic/app.py", line 1209, in create_server
asyncio_server_kwargs=asyncio_server_kwargs, **server_settings
TypeError: object NoneType can't be used in 'await' expression
sys:1: RuntimeWarning: coroutine 'Loop.create_server' was never awaited |
a need to return an asyncio.Server or run it right away
support all necessary features like "start_serving",
so, in order to make sanic work well with asyncio from 3.7
there's a need to introduce generic way for passing
kwargs for "loop.create_server"
Closes: #1469