Browse Source

updated asgi examples to the latest uvicorn

pull/319/head
Miguel Grinberg 6 years ago
parent
commit
f86999ff9c
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 10
      examples/server/asgi/app.py
  2. 2
      examples/server/asgi/latency.py
  3. 10
      examples/server/asgi/requirements.txt

10
examples/server/asgi/app.py

@ -2,7 +2,6 @@
import asyncio import asyncio
import uvicorn import uvicorn
from uvicorn.loops.auto import auto_loop_setup
import socketio import socketio
@ -10,6 +9,7 @@ sio = socketio.AsyncServer(async_mode='asgi')
app = socketio.ASGIApp(sio, static_files={ app = socketio.ASGIApp(sio, static_files={
'/': {'content_type': 'text/html', 'filename': 'app.html'}, '/': {'content_type': 'text/html', 'filename': 'app.html'},
}) })
background_task_started = False
async def background_task(): async def background_task():
@ -68,6 +68,10 @@ async def disconnect_request(sid):
@sio.on('connect', namespace='/test') @sio.on('connect', namespace='/test')
async def test_connect(sid, environ): async def test_connect(sid, environ):
global background_task_started
if not background_task_started:
sio.start_background_task(background_task)
background_task_started = True
await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid, await sio.emit('my response', {'data': 'Connected', 'count': 0}, room=sid,
namespace='/test') namespace='/test')
@ -78,6 +82,4 @@ def test_disconnect(sid):
if __name__ == '__main__': if __name__ == '__main__':
loop = auto_loop_setup() uvicorn.run(app, host='127.0.0.1', port=5000)
sio.start_background_task(background_task)
uvicorn.run(app, '127.0.0.1', 5000, loop=loop)

2
examples/server/asgi/latency.py

@ -17,4 +17,4 @@ async def ping(sid):
if __name__ == '__main__': if __name__ == '__main__':
uvicorn.run(app, '127.0.0.1', 5000) uvicorn.run(app, host='127.0.0.1', port=5000)

10
examples/server/asgi/requirements.txt

@ -1,9 +1,9 @@
Click==7.0 Click==7.0
h11==0.8.1 h11==0.8.1
httptools==0.0.11 httptools==0.0.13
python-engineio==3.0.0 python-engineio
-e [email protected]:miguelgrinberg/python-socketio@b214380d056dbbfb08273ac482633254176cb847#egg=python_socketio python_socketio
six==1.11.0 six==1.11.0
uvicorn==0.3.21 uvicorn==0.7.0
uvloop==0.11.3 uvloop==0.12.2
websockets==7.0 websockets==7.0

Loading…
Cancel
Save