@@ -252,15 +252,15 @@ <h2 id="synchronous-asynchronous-execution">Synchronous & Asynchronous Execu
252
252
Task 9 done
253
253
Asynchronous:
254
254
Task 0 done
255
- Task 9 done
256
- Task 1 done
257
- Task 4 done
258
255
Task 6 done
259
- Task 5 done
260
- Task 8 done
261
256
Task 3 done
262
- Task 2 done
263
257
Task 7 done
258
+ Task 2 done
259
+ Task 1 done
260
+ Task 8 done
261
+ Task 5 done
262
+ Task 9 done
263
+ Task 4 done
264
264
</ pre > </ code > </ p >
265
265
< p > In the synchronous case all the tasks are run sequentially,
266
266
which results in the main programming < em > blocking</ em > (
@@ -281,9 +281,9 @@ <h2 id="synchronous-asynchronous-execution">Synchronous & Asynchronous Execu
281
281
whole queue. In the async case the maximum runtime is roughly 2
282
282
seconds since none of the tasks block the execution of the
283
283
others.</ p >
284
- < p > A more common use case, fetching data from a server
285
- asynchronously, the runtime of < code > fetch()</ code > will differ between
286
- requests given the load on the remote server.</ p >
284
+ < p > In a more common use case, asynchronously fetching data from a server,
285
+ the runtime of < code > fetch()</ code > will differ between
286
+ requests, depending on the load on the remote server at the time of the request .</ p >
287
287
< pre > < code class ="python "> import gevent.monkey
288
288
gevent.monkey.patch_socket()
289
289
@@ -320,9 +320,9 @@ <h2 id="synchronous-asynchronous-execution">Synchronous & Asynchronous Execu
320
320
321
321
< h2 id ="determinism "> Determinism</ h2 >
322
322
< p > As mentioned previously, greenlets are deterministic. Given the same
323
- configuration of greenlets and the same set of inputs and they always
324
- produce the same output. For example lets spread a task across a
325
- multiprocessing pool compared to a gevent pool.</ p >
323
+ configuration of greenlets and the same set of inputs, they always
324
+ produce the same output. For example, let's spread a task across a
325
+ multiprocessing pool and compare its results to the one of a gevent pool.</ p >
326
326
< pre >
327
327
< code class ="python ">
328
328
import time
@@ -369,14 +369,13 @@ <h2 id="determinism">Determinism</h2>
369
369
concurrency", they still can experience some of the same problems
370
370
that POSIX threads and processes experience.</ p >
371
371
< p > The perennial problem involved with concurrency is known as a
372
- < em > race condition</ em > . Simply put is when two concurrent threads
372
+ < em > race condition</ em > . Simply put, a race condition occurs when two concurrent threads
373
373
/ processes depend on some shared resource but also attempt to
374
- modify this value. This results in resources whose values become
374
+ modify this value. This results in resources which values become
375
375
time-dependent on the execution order. This is a problem, and in
376
376
general one should very much try to avoid race conditions since
377
- they result program behavior which is globally
378
- non-deterministic.</ p >
379
- < p > The best approach to this is to simply avoid all global state all
377
+ they result in a globally non-deterministic program behavior.</ p >
378
+ < p > The best approach to this is to simply avoid all global state at all
380
379
times. Global state and import-time side effects will always come
381
380
back to bite you!</ p >
382
381
< h2 id ="spawning-greenlets "> Spawning Greenlets</ h2 >
@@ -445,17 +444,16 @@ <h2 id="spawning-greenlets">Spawning Greenlets</h2>
445
444
< h2 id ="greenlet-state "> Greenlet State</ h2 >
446
445
< p > Like any other segment of code, Greenlets can fail in various
447
446
ways. A greenlet may fail to throw an exception, fail to halt or
448
- consume too many system resources.</ p > </ p >
447
+ consume too many system resources.</ p >
449
448
< p > The internal state of a greenlet is generally a time-dependent
450
449
parameter. There are a number of flags on greenlets which let
451
- you monitor the state of the thread</ p >
452
-
450
+ you monitor the state of the thread:</ p >
453
451
< ul >
454
- < li > < code > started</ code > -- Boolean, indicates whether the Greenlet has been started. </ li > </ li >
455
- < li > < code > ready()</ code > -- Boolean, indicates whether the Greenlet has halted</ li > </ li >
456
- < li > < code > successful()</ code > -- Boolean, indicates whether the Greenlet has halted and not thrown an exception</ li > </ li >
457
- < li > < code > value</ code > -- arbitrary, the value returned by the Greenlet</ li > </ li >
458
- < li > < code > exception</ code > -- exception, uncaught exception instance thrown inside the greenlet</ li > </ li >
452
+ < li > < code > started</ code > -- Boolean, indicates whether the Greenlet has been started</ li >
453
+ < li > < code > ready()</ code > -- Boolean, indicates whether the Greenlet has halted</ li >
454
+ < li > < code > successful()</ code > -- Boolean, indicates whether the Greenlet has halted and not thrown an exception</ li >
455
+ < li > < code > value</ code > -- arbitrary, the value returned by the Greenlet</ li >
456
+ < li > < code > exception</ code > -- exception, uncaught exception instance thrown inside the greenlet</ li >
459
457
</ ul >
460
458
< pre > < code class ="python ">
461
459
import gevent
@@ -556,7 +554,7 @@ <h2 id="timeouts">Timeouts</h2>
556
554
</ code >
557
555
</ pre >
558
556
559
- < p > Or with a context manager in a < code > with</ code > statement.</ p >
557
+ < p > They can also be used with a context manager, in a < code > with</ code > statement.</ p >
560
558
< pre >
561
559
< code class ="python "> import gevent
562
560
from gevent import Timeout
@@ -642,8 +640,8 @@ <h2 id="monkeypatching">Monkeypatching</h2>
642
640
After monkey patch
643
641
class 'gevent.socket.socket'
644
642
645
- After monkey patch
646
643
built-in function select
644
+ After monkey patch
647
645
function select at 0x1924de8
648
646
</ code >
649
647
</ pre >
@@ -664,7 +662,7 @@ <h2 id="monkeypatching">Monkeypatching</h2>
664
662
of our gevent stack.</ p >
665
663
< p > This lets us integrate libraries that would not normally work with
666
664
gevent without ever writing a single line of code. While monkey-patching
667
- is still evil, in this case it is a "usefull evil".</ p >
665
+ is still evil, in this case it is a "useful evil".</ p >
668
666
< h1 id ="data-structures "> Data Structures</ h1 >
669
667
< h2 id ="events "> Events</ h2 >
670
668
< p > Events are a form of asynchronous communication between
@@ -801,7 +799,7 @@ <h2 id="queues">Queues</h2>
801
799
either < code > gevent.queue.Empty</ code > or
802
800
< code > gevent.queue.Full</ code > in the operation is not possible.</ p >
803
801
< p > In this example we have the boss running simultaneously to the
804
- workers and have a restriction on the Queue that it can contain no
802
+ workers and have a restriction on the Queue preventing it from containing
805
803
more than three elements. This restriction means that the < code > put</ code >
806
804
operation will block until there is space on the queue.
807
805
Conversely the < code > get</ code > operation will block if there are
@@ -910,9 +908,8 @@ <h2 id="groups-and-pools">Groups and Pools</h2>
910
908
fizz
911
909
fizz
912
910
</ pre > </ code > </ p >
913
- < p > This is very usefull for managing groups of asynchronous tasks
914
- that.</ p >
915
- < p > As mentioned above Group also provides an API for dispatching
911
+ < p > This is very useful for managing groups of asynchronous tasks.</ p >
912
+ < p > As mentioned above, < code > Group</ code > also provides an API for dispatching
916
913
jobs to grouped greenlets and collecting their results in various
917
914
ways.</ p >
918
915
< pre > < code class ="python ">
@@ -949,11 +946,11 @@ <h2 id="groups-and-pools">Groups and Pools</h2>
949
946
< p > </ code >
950
947
< pre > < code class ="python ">
951
948
Size of group 3
952
- Hello from Greenlet 42121488
949
+ Hello from Greenlet 20398800
953
950
Size of group 3
954
- Hello from Greenlet 42119248
951
+ Hello from Greenlet 20399280
955
952
Size of group 3
956
- Hello from Greenlet 42120208
953
+ Hello from Greenlet 20400240
957
954
Ordered
958
955
('task', 0)
959
956
('task', 1)
@@ -969,7 +966,6 @@ <h2 id="groups-and-pools">Groups and Pools</h2>
969
966
tasks in parallel.</ p >
970
967
< pre > < code class ="python ">
971
968
import gevent
972
- from gevent import getcurrent
973
969
from gevent.pool import Pool
974
970
975
971
pool = Pool(2)
@@ -1065,8 +1061,8 @@ <h2 id="locks-and-semaphores">Locks and Semaphores</h2>
1065
1061
ensure that resources are only in use at one time in the context
1066
1062
of a program.</ p >
1067
1063
< h2 id ="thread-locals "> Thread Locals</ h2 >
1068
- < p > Gevnet also allows you to specify data which is local the
1069
- greenlet context. Internally this is implemented as a global
1064
+ < p > Gevent also allows you to specify data which is local to the
1065
+ greenlet context. Internally, this is implemented as a global
1070
1066
lookup which addresses a private namespace keyed by the
1071
1067
greenlet's < code > getcurrent()</ code > value.</ p >
1072
1068
< pre > < code class ="python ">
@@ -1142,8 +1138,8 @@ <h2 id="thread-locals">Thread Locals</h2>
1142
1138
< code >
1143
1139
</ pre >
1144
1140
1145
- < p > Flask's system is more a bit sophisticated than this example, but the
1146
- idea of using thread locals as local session storage is nontheless the
1141
+ < p > Flask's system is a bit more sophisticated than this example, but the
1142
+ idea of using thread locals as local session storage is nonetheless the
1147
1143
same.</ p >
1148
1144
< h2 id ="subprocess "> Subprocess</ h2 >
1149
1145
< p > As of Gevent 1.0, support has been added for cooperative waiting
0 commit comments