Skip to content

Commit

Permalink
a few improvements to examples
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 28, 2016
1 parent 065cc4b commit 20185c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
26 changes: 9 additions & 17 deletions examples/app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
# set this to 'threading', 'eventlet', or 'gevent'
async_mode = 'threading'

if async_mode == 'eventlet':
import eventlet
eventlet.monkey_patch()
elif async_mode == 'gevent':
from gevent import monkey
monkey.patch_all()
# set async_mode to 'threading', 'eventlet' or 'gevent' to force a mode
# else, the best mode is selected automatically from what's installed
async_mode = None

import time
from threading import Thread
from flask import Flask, render_template
import socketio

Expand All @@ -24,7 +17,7 @@ def background_thread():
"""Example of how to send server generated events to clients."""
count = 0
while True:
time.sleep(10)
sio.sleep(10)
count += 1
sio.emit('my response', {'data': 'Server generated event'},
namespace='/test')
Expand All @@ -34,8 +27,7 @@ def background_thread():
def index():
global thread
if thread is None:
thread = Thread(target=background_thread)
thread.start()
thread = sio.start_background_task(background_thread)
return render_template('index.html')


Expand Down Expand Up @@ -95,14 +87,14 @@ def test_disconnect(sid):


if __name__ == '__main__':
if async_mode == 'threading':
if sio.async_mode == 'threading':
# deploy with Werkzeug
app.run(threaded=True)
elif async_mode == 'eventlet':
elif sio.async_mode == 'eventlet':
# deploy with eventlet
import eventlet
eventlet.wsgi.server(eventlet.listen(('', 5000)), app)
elif async_mode == 'gevent':
elif sio.async_mode == 'gevent':
# deploy with gevent
from gevent import pywsgi
try:
Expand All @@ -116,4 +108,4 @@ def test_disconnect(sid):
else:
pywsgi.WSGIServer(('', 5000), app).serve_forever()
else:
print('Unknown async_mode: ' + async_mode)
print('Unknown async_mode: ' + sio.async_mode)
17 changes: 9 additions & 8 deletions examples/latency.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import socketio

# set this to 'threading', 'eventlet', or 'gevent'
async_mode = 'threading'
# set async_mode to 'threading', 'eventlet' or 'gevent' to force a mode
# else, the best mode is selected automatically from what's installed
async_mode = None

sio = socketio.Server(async_mode=async_mode)
app = Flask(__name__)
Expand All @@ -15,21 +16,21 @@ def index():
return render_template('latency.html')


@sio.on('ping')
@sio.on('ping_from_client')
def ping(sid):
sio.emit('pong', room=sid)
sio.emit('pong_from_server', room=sid)


if __name__ == '__main__':
if async_mode == 'threading':
if sio.async_mode == 'threading':
# deploy with Werkzeug
app.run(threaded=True)
elif async_mode == 'eventlet':
elif sio.async_mode == 'eventlet':
# deploy with eventlet
import eventlet
from eventlet import wsgi
wsgi.server(eventlet.listen(('', 5000)), app)
elif async_mode == 'gevent':
elif sio.async_mode == 'gevent':
# deploy with gevent
from gevent import pywsgi
try:
Expand All @@ -43,4 +44,4 @@ def ping(sid):
else:
pywsgi.WSGIServer(('', 5000), app).serve_forever()
else:
print('Unknown async_mode: ' + async_mode)
print('Unknown async_mode: ' + sio.async_mode)

0 comments on commit 20185c9

Please sign in to comment.