@@ -57,6 +57,7 @@ <h3 class="author">
5757< li > < a href ="#greenlet-state "> Greenlet State</ a > </ li >
5858< li > < a href ="#program-shutdown "> Program Shutdown</ a > </ li >
5959< li > < a href ="#timeouts "> Timeouts</ a > </ li >
60+ < li > < a href ="#monkeypatching "> Monkeypatching</ a > </ li >
6061</ ul >
6162</ li >
6263< li > < a href ="#data-structures "> Data Structures</ a > < ul >
@@ -250,16 +251,16 @@ <h2 id="synchronous-asynchronous-execution">Synchronous & Asynchronous Execu
250251Task 8 done
251252Task 9 done
252253Asynchronous:
254+ Task 0 done
255+ Task 9 done
253256Task 1 done
254- Task 2 done
257+ Task 4 done
258+ Task 6 done
255259Task 5 done
256- Task 7 done
257- Task 9 done
258260Task 8 done
259- Task 0 done
260261Task 3 done
261- Task 4 done
262- Task 6 done
262+ Task 2 done
263+ Task 7 done
263264</ pre > </ code > </ p >
264265< p > In the synchronous case all the tasks are run sequentially,
265266which results in the main programming < em > blocking</ em > (
@@ -612,6 +613,58 @@ <h2 id="timeouts">Timeouts</h2>
612613Thread 2 timed out
613614Thread 3 timed out
614615</ pre > </ code > </ p >
616+ < h2 id ="monkeypatching "> Monkeypatching</ h2 >
617+ < p > Alas we come to dark corners of Gevent. I've avoided mentioning
618+ monkey patching up until now to try and motivate the powerful
619+ coroutine patterns but the time has come to discuss the dark arts
620+ of monkey-patching. If you noticed above we invoked the commnad
621+ < code > monkey.patch_socket()</ code > . This is a purely side-effectful command to
622+ modify the standard library's socket library</ p >
623+ < pre >
624+ < code class ="python "> import socket
625+ print( socket.socket )
626+
627+ print "After monkey patch"
628+ from gevent import monkey
629+ monkey.patch_socket()
630+ print( socket.socket )
631+
632+ import select
633+ print select.select
634+ monkey.patch_select()
635+ print "After monkey patch"
636+ print( select.select )
637+ </ code >
638+ </ pre >
639+
640+ < pre >
641+ < code class ="python "> class 'socket.socket'
642+ After monkey patch
643+ class 'gevent.socket.socket'
644+
645+ After monkey patch
646+ built-in function select
647+ function select at 0x1924de8
648+ </ code >
649+ </ pre >
650+
651+ < p > Python's runtime allows for most objects to be modified at runtime
652+ including modules, classes, and even functions. This is generally an
653+ astoudingly bad idea since it creates an "implicit side-effect" that is
654+ most often extremely difficult to debug if problems occur, nevertheless
655+ in extreme situations where a library needs to alter the fundamental
656+ behavior of Python itself monkey patches can be used. In this case gevent
657+ is capable of patching most of the blocking system calls in the standard
658+ library including those in < code > socket</ code > , < code > ssl</ code > , < code > threading</ code > and
659+ < code > select</ code > modules to instead behave cooperatively.</ p >
660+ < p > For example, the Redis python bindings normally uses regular tcp
661+ sockets to communicate with the < code > redis-server</ code > instance. Simply
662+ by invoking < code > gevent.monkey.patch_all()</ code > we can make the redis
663+ bindings schedule requests cooperatively and work with the rest
664+ of our gevent stack.</ p >
665+ < p > This lets us integrate libraries that would not normally work with
666+ 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 >
615668< h1 id ="data-structures "> Data Structures</ h1 >
616669< h2 id ="events "> Events</ h2 >
617670< p > Events are a form of asynchronous communication between
@@ -896,11 +949,11 @@ <h2 id="groups-and-pools">Groups and Pools</h2>
896949< p > </ code >
897950< pre > < code class ="python ">
898951Size of group 3
899- Hello from Greenlet 41900624
952+ Hello from Greenlet 42121488
900953Size of group 3
901- Hello from Greenlet 41900944
954+ Hello from Greenlet 42119248
902955Size of group 3
903- Hello from Greenlet 41899024
956+ Hello from Greenlet 42120208
904957Ordered
905958('task', 0)
906959('task', 1)
0 commit comments