Browse Source

a few improvements to examples

pull/31/head
Miguel Grinberg 9 years ago
parent
commit
20185c9a95
  1. 26
      examples/app.py
  2. 17
      examples/latency.py

26
examples/app.py

@ -1,15 +1,8 @@
# set this to 'threading', 'eventlet', or 'gevent' # set async_mode to 'threading', 'eventlet' or 'gevent' to force a mode
async_mode = 'threading' # else, the best mode is selected automatically from what's installed
async_mode = None
if async_mode == 'eventlet':
import eventlet
eventlet.monkey_patch()
elif async_mode == 'gevent':
from gevent import monkey
monkey.patch_all()
import time import time
from threading import Thread
from flask import Flask, render_template from flask import Flask, render_template
import socketio import socketio
@ -24,7 +17,7 @@ def background_thread():
"""Example of how to send server generated events to clients.""" """Example of how to send server generated events to clients."""
count = 0 count = 0
while True: while True:
time.sleep(10) sio.sleep(10)
count += 1 count += 1
sio.emit('my response', {'data': 'Server generated event'}, sio.emit('my response', {'data': 'Server generated event'},
namespace='/test') namespace='/test')
@ -34,8 +27,7 @@ def background_thread():
def index(): def index():
global thread global thread
if thread is None: if thread is None:
thread = Thread(target=background_thread) thread = sio.start_background_task(background_thread)
thread.start()
return render_template('index.html') return render_template('index.html')
@ -95,14 +87,14 @@ def test_disconnect(sid):
if __name__ == '__main__': if __name__ == '__main__':
if async_mode == 'threading': if sio.async_mode == 'threading':
# deploy with Werkzeug # deploy with Werkzeug
app.run(threaded=True) app.run(threaded=True)
elif async_mode == 'eventlet': elif sio.async_mode == 'eventlet':
# deploy with eventlet # deploy with eventlet
import eventlet import eventlet
eventlet.wsgi.server(eventlet.listen(('', 5000)), app) eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
elif async_mode == 'gevent': elif sio.async_mode == 'gevent':
# deploy with gevent # deploy with gevent
from gevent import pywsgi from gevent import pywsgi
try: try:
@ -116,4 +108,4 @@ if __name__ == '__main__':
else: else:
pywsgi.WSGIServer(('', 5000), app).serve_forever() pywsgi.WSGIServer(('', 5000), app).serve_forever()
else: else:
print('Unknown async_mode: ' + async_mode) print('Unknown async_mode: ' + sio.async_mode)

17
examples/latency.py

@ -2,8 +2,9 @@ from flask import Flask, render_template
import socketio import socketio
# set this to 'threading', 'eventlet', or 'gevent' # set async_mode to 'threading', 'eventlet' or 'gevent' to force a mode
async_mode = 'threading' # else, the best mode is selected automatically from what's installed
async_mode = None
sio = socketio.Server(async_mode=async_mode) sio = socketio.Server(async_mode=async_mode)
app = Flask(__name__) app = Flask(__name__)
@ -15,21 +16,21 @@ def index():
return render_template('latency.html') return render_template('latency.html')
@sio.on('ping') @sio.on('ping_from_client')
def ping(sid): def ping(sid):
sio.emit('pong', room=sid) sio.emit('pong_from_server', room=sid)
if __name__ == '__main__': if __name__ == '__main__':
if async_mode == 'threading': if sio.async_mode == 'threading':
# deploy with Werkzeug # deploy with Werkzeug
app.run(threaded=True) app.run(threaded=True)
elif async_mode == 'eventlet': elif sio.async_mode == 'eventlet':
# deploy with eventlet # deploy with eventlet
import eventlet import eventlet
from eventlet import wsgi from eventlet import wsgi
wsgi.server(eventlet.listen(('', 5000)), app) wsgi.server(eventlet.listen(('', 5000)), app)
elif async_mode == 'gevent': elif sio.async_mode == 'gevent':
# deploy with gevent # deploy with gevent
from gevent import pywsgi from gevent import pywsgi
try: try:
@ -43,4 +44,4 @@ if __name__ == '__main__':
else: else:
pywsgi.WSGIServer(('', 5000), app).serve_forever() pywsgi.WSGIServer(('', 5000), app).serve_forever()
else: else:
print('Unknown async_mode: ' + async_mode) print('Unknown async_mode: ' + sio.async_mode)

Loading…
Cancel
Save