Skip to content

Commit 0ab4e77

Browse files
committed
Merge branch 'master' into gh-pages
Conflicts: index.html
2 parents e695d30 + 8656357 commit 0ab4e77

3 files changed

Lines changed: 178 additions & 14 deletions

File tree

body.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
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

index.html

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
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 &amp; 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
177181
knowledge of Python but not much else. No knowledge of
178182
concurrency 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&eacute;r&eacute;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
183202
lightweight coroutine provided to Python as a C extension module.
@@ -313,15 +332,15 @@ <h2 id="synchronous-asynchronous-execution">Synchronous &amp; Asynchronous Execu
313332
Task 8 done
314333
Task 9 done
315334
Asynchronous:
316-
Task 2 done
335+
Task 4 done
317336
Task 5 done
318-
Task 6 done
337+
Task 2 done
319338
Task 0 done
320-
Task 3 done
321-
Task 8 done
322-
Task 4 done
323339
Task 9 done
324340
Task 7 done
341+
Task 3 done
342+
Task 6 done
343+
Task 8 done
325344
Task 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>&lt;html&gt;
1203+
&lt;head&gt;
1204+
&lt;title&gt;Minimal websocket application&lt;/title&gt;
1205+
&lt;script type="text/javascript" src="jquery.min.js"&gt;&lt;/script&gt;
1206+
&lt;script type="text/javascript"&gt;
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('&lt;p&gt;' + evt.data + '&lt;/p&gt;')
1214+
}
1215+
// Just update our conn_status field with the connection status
1216+
ws.onopen = function(evt) {
1217+
$('#conn_status').html('&lt;b&gt;Connected&lt;/b&gt;');
1218+
}
1219+
ws.onerror = function(evt) {
1220+
$('#conn_status').html('&lt;b&gt;Error&lt;/b&gt;');
1221+
}
1222+
ws.onclose = function(evt) {
1223+
$('#conn_status').html('&lt;b&gt;Closed&lt;/b&gt;');
1224+
}
1225+
});
1226+
&lt;/script&gt;
1227+
&lt;/head&gt;
1228+
&lt;body&gt;
1229+
&lt;h1&gt;WebSocket Example&lt;/h1&gt;
1230+
&lt;div id="conn_status"&gt;Not Connected&lt;/div&gt;
1231+
&lt;div id="placeholder" style="width:600px;height:300px;"&gt;&lt;/div&gt;
1232+
&lt;/body&gt;
1233+
&lt;/html&gt;
1234+
</code></pre>
11551235
<h2 id="chat-server">Chat Server</h2>
11561236
<p>The final motivating example, a realtime chat room. This example
11571237
requires <a href="http://flask.pocoo.org/">Flask</a> ( but not neccesarily so, you could use Django,

tutorial.md

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
[TOC]
22

3-
# Core
3+
# Introduction
44

55
The structure of this tutorial assumes an intermediate level
66
knowledge of Python but not much else. No knowledge of
77
concurrency is expected. The goal is to give you
8-
the tools you need to get going with gevent and use it to solve
9-
or speed up your applications today.
8+
the tools you need to get going with gevent, help you tame
9+
your existing concurrency problems and start writing asynchronous
10+
applications today.
11+
12+
### Contributors
13+
14+
In chronological order of contribution:
15+
[Stephen Diehl](http://www.stephendiehl.com)
16+
[J&eacute;r&eacute;my Bethmont](https://github.com/jerem)
17+
[sww](https://github.com/sww)
18+
[Bruno Bigras](https://github.com/brunoqc)
19+
[David Ripton](https://github.com/dripton)
20+
[Travis Cline](https://github.com/traviscline)
21+
[Boris Feld](https://github.com/Lothiraldan)
22+
23+
This is a collaborative document published under MIT license.
24+
Have something to add? See a typo? Fork and issue a
25+
pull request [Github](https://github.com/sdiehl/gevent-tutorial).
26+
Any and all contributions are welcome.
27+
28+
# Core
1029

1130
## Greenlets
1231

@@ -903,10 +922,75 @@ gevent.spawn(producer)
903922
WSGIServer(('', 8000), ajax_endpoint).serve_forever()
904923

905924
</code>
906-
</pre>
925+
</pre>
907926

908927
## Websockets
909928

929+
Websocket example which requires <a href="https://bitbucket.org/Jeffrey/gevent-websocket/src">gevent-websocket</a>.
930+
931+
932+
<pre>
933+
<code class="python"># Simple gevent-websocket server
934+
import json
935+
import random
936+
937+
from gevent import pywsgi, sleep
938+
from geventwebsocket.handler import WebSocketHandler
939+
940+
class WebSocketApp(object):
941+
'''Send random data to the websocket'''
942+
943+
def __call__(self, environ, start_response):
944+
ws = environ['wsgi.websocket']
945+
x = 0
946+
while True:
947+
data = json.dumps({'x': x, 'y': random.randint(1, 5)})
948+
ws.send(data)
949+
x += 1
950+
sleep(0.5)
951+
952+
server = pywsgi.WSGIServer(("", 10000), WebSocketApp(),
953+
handler_class=WebSocketHandler)
954+
server.serve_forever()
955+
</code>
956+
</pre>
957+
958+
HTML Page:
959+
960+
<html>
961+
<head>
962+
<title>Minimal websocket application</title>
963+
<script type="text/javascript" src="jquery.min.js"></script>
964+
<script type="text/javascript">
965+
$(function() {
966+
// Open up a connection to our server
967+
var ws = new WebSocket("ws://localhost:10000/");
968+
969+
// What do we do when we get a message?
970+
ws.onmessage = function(evt) {
971+
$("#placeholder").append('<p>' + evt.data + '</p>')
972+
}
973+
// Just update our conn_status field with the connection status
974+
ws.onopen = function(evt) {
975+
$('#conn_status').html('<b>Connected</b>');
976+
}
977+
ws.onerror = function(evt) {
978+
$('#conn_status').html('<b>Error</b>');
979+
}
980+
ws.onclose = function(evt) {
981+
$('#conn_status').html('<b>Closed</b>');
982+
}
983+
});
984+
</script>
985+
</head>
986+
<body>
987+
<h1>WebSocket Example</h1>
988+
<div id="conn_status">Not Connected</div>
989+
<div id="placeholder" style="width:600px;height:300px;"></div>
990+
</body>
991+
</html>
992+
993+
910994
## Chat Server
911995

912996
The final motivating example, a realtime chat room. This example

0 commit comments

Comments
 (0)