128128< header >
129129 < h1 > < span class ="green "> gevent</ span > For the Working Python Developer</ h1 >
130130 < h3 class ="author ">
131- < a href =" http://www.stephendiehl.com " > Stephen Diehl </ a >
131+ Written by the Gevent Community
132132 </ h3 >
133133</ header >
134134
@@ -138,6 +138,10 @@ <h3 class="author">
138138
139139< div class ="toc ">
140140< ul >
141+ < li > < a href ="#introduction "> Introduction</ a > < ul >
142+ < li > < a href ="#contributors "> Contributors</ a > </ li >
143+ </ ul >
144+ </ li >
141145< li > < a href ="#core "> Core</ a > < ul >
142146< li > < a href ="#greenlets "> Greenlets</ a > </ li >
143147< li > < a href ="#synchronous-asynchronous-execution "> Synchronous & Asynchronous Execution</ a > </ li >
@@ -172,12 +176,27 @@ <h3 class="author">
172176</ li >
173177</ ul >
174178</ div >
175- < h1 id ="core " > Core </ h1 >
179+ < h1 id ="introduction " > Introduction </ h1 >
176180< p > The structure of this tutorial assumes an intermediate level
177181knowledge of Python but not much else. No knowledge of
178182concurrency is expected. The goal is to give you
179- the tools you need to get going with gevent and use it to solve
180- or speed up your applications today.</ p >
183+ the tools you need to get going with gevent, help you tame
184+ your existing concurrency problems and start writing asynchronous
185+ applications today.</ p >
186+ < h3 id ="contributors "> Contributors</ h3 >
187+ < p > In chronological order of contribution:
188+ < a href ="http://www.stephendiehl.com "> Stephen Diehl</ a >
189+ < a href ="https://github.com/jerem "> Jérémy Bethmont</ a >
190+ < a href ="https://github.com/sww "> sww</ a >
191+ < a href ="https://github.com/brunoqc "> Bruno Bigras</ a >
192+ < a href ="https://github.com/dripton "> David Ripton</ a >
193+ < a href ="https://github.com/traviscline "> Travis Cline</ a >
194+ < a href ="https://github.com/Lothiraldan "> Boris Feld</ a > </ p >
195+ < p > This is a collaborative document published under MIT license.
196+ Have something to add? See a typo? Fork and issue a
197+ pull request < a href ="https://github.com/sdiehl/gevent-tutorial "> Github</ a > .
198+ Any and all contributions are welcome.</ p >
199+ < h1 id ="core "> Core</ h1 >
181200< h2 id ="greenlets "> Greenlets</ h2 >
182201< p > The primary pattern used in gevent is the < strong > Greenlet</ strong > , a
183202lightweight coroutine provided to Python as a C extension module.
@@ -313,15 +332,15 @@ <h2 id="synchronous-asynchronous-execution">Synchronous & Asynchronous Execu
313332Task 8 done
314333Task 9 done
315334Asynchronous:
316- Task 2 done
335+ Task 4 done
317336Task 5 done
318- Task 6 done
337+ Task 2 done
319338Task 0 done
320- Task 3 done
321- Task 8 done
322- Task 4 done
323339Task 9 done
324340Task 7 done
341+ Task 3 done
342+ Task 6 done
343+ Task 8 done
325344Task 1 done
326345</ pre > </ code > </ p >
327346< p > In the synchronous case all the tasks are run sequentially,
@@ -1152,6 +1171,67 @@ <h2 id="long-polling">Long Polling</h2>
11521171</ pre >
11531172
11541173< h2 id ="websockets "> Websockets</ h2 >
1174+ < p > Websocket example which requires < a href ="https://bitbucket.org/Jeffrey/gevent-websocket/src "> gevent-websocket</ a > .</ p >
1175+ < pre >
1176+ < code class ="python "> # Simple gevent-websocket server
1177+ import json
1178+ import random
1179+
1180+ from gevent import pywsgi, sleep
1181+ from geventwebsocket.handler import WebSocketHandler
1182+
1183+ class WebSocketApp(object):
1184+ '''Send random data to the websocket'''
1185+
1186+ def __call__(self, environ, start_response):
1187+ ws = environ['wsgi.websocket']
1188+ x = 0
1189+ while True:
1190+ data = json.dumps({'x': x, 'y': random.randint(1, 5)})
1191+ ws.send(data)
1192+ x += 1
1193+ sleep(0.5)
1194+
1195+ server = pywsgi.WSGIServer(("", 10000), WebSocketApp(),
1196+ handler_class=WebSocketHandler)
1197+ server.serve_forever()
1198+ </ code >
1199+ </ pre >
1200+
1201+ < p > HTML Page:</ p >
1202+ < pre > < code > <html>
1203+ <head>
1204+ <title>Minimal websocket application</title>
1205+ <script type="text/javascript" src="jquery.min.js"></script>
1206+ <script type="text/javascript">
1207+ $(function() {
1208+ // Open up a connection to our server
1209+ var ws = new WebSocket("ws://localhost:10000/");
1210+
1211+ // What do we do when we get a message?
1212+ ws.onmessage = function(evt) {
1213+ $("#placeholder").append('<p>' + evt.data + '</p>')
1214+ }
1215+ // Just update our conn_status field with the connection status
1216+ ws.onopen = function(evt) {
1217+ $('#conn_status').html('<b>Connected</b>');
1218+ }
1219+ ws.onerror = function(evt) {
1220+ $('#conn_status').html('<b>Error</b>');
1221+ }
1222+ ws.onclose = function(evt) {
1223+ $('#conn_status').html('<b>Closed</b>');
1224+ }
1225+ });
1226+ </script>
1227+ </head>
1228+ <body>
1229+ <h1>WebSocket Example</h1>
1230+ <div id="conn_status">Not Connected</div>
1231+ <div id="placeholder" style="width:600px;height:300px;"></div>
1232+ </body>
1233+ </html>
1234+ </ code > </ pre >
11551235< h2 id ="chat-server "> Chat Server</ h2 >
11561236< p > The final motivating example, a realtime chat room. This example
11571237requires < a href ="http://flask.pocoo.org/ "> Flask</ a > ( but not neccesarily so, you could use Django,
0 commit comments