@@ -258,7 +258,9 @@ executor when the task is computationally expensive.
258
258
There are two main ways of executing things in parallel using the two
259
259
Executors. One way is with the `map(func, iterables) ` method. This works
260
260
almost exactly like the builtin `map() ` function, except it will execute
261
- everything in parallel. ::
261
+ everything in parallel. :
262
+
263
+ .. code-block :: python
262
264
263
265
from concurrent.futures import ThreadPoolExecutor
264
266
import requests
@@ -301,7 +303,8 @@ add_done_callback(fn)
301
303
Attach a callback function that will be executed (as `fn(future) `) when the
302
304
scheduled callable returns.
303
305
304
- ::
306
+
307
+ .. code-block :: python
305
308
306
309
from concurrent.futures import ProcessPoolExecutor, as_completed
307
310
@@ -355,7 +358,9 @@ The standard library comes with a `threading`_ module that allows a user to
355
358
work with multiple threads manually.
356
359
357
360
Running a function in another thread is as simple as passing a callable and
358
- it's arguments to `Thread `'s constructor and then calling `start() `::
361
+ it's arguments to `Thread `'s constructor and then calling `start() `:
362
+
363
+ .. code-block :: python
359
364
360
365
from threading import Thread
361
366
import requests
@@ -367,12 +372,16 @@ it's arguments to `Thread`'s constructor and then calling `start()`::
367
372
some_thread = Thread(get_webpage, ' http://google.com/' )
368
373
some_thread.start()
369
374
370
- To wait until the thread has terminated, call `join() `::
375
+ To wait until the thread has terminated, call `join() `:
376
+
377
+ .. code-block :: python
371
378
372
379
some_thread.join()
373
380
374
381
After calling `join() `, it is always a good idea to check whether the thread is
375
- still alive (because the join call timed out)::
382
+ still alive (because the join call timed out):
383
+
384
+ .. code-block :: python
376
385
377
386
if some_thread.is_alive():
378
387
print (" join() must have timed out." )
@@ -389,7 +398,10 @@ which are difficult to debug. A good example is this `stackoverflow post`_.
389
398
The way this can be avoided is by using a `Lock `_ that each thread needs to
390
399
acquire before writing to a shared resource. Locks can be acquired and released
391
400
through either the contextmanager protocol (`with ` statement), or by using
392
- `acquire() ` and `release() ` directly. Here is a (rather contrived) example::
401
+ `acquire() ` and `release() ` directly. Here is a (rather contrived) example:
402
+
403
+
404
+ .. code-block :: python
393
405
394
406
from threading import Lock, Thread
395
407
@@ -402,7 +414,8 @@ through either the contextmanager protocol (`with` statement), or by using
402
414
403
415
def monitor_website (some_website ):
404
416
"""
405
- Monitor a website and then if there are any changes, log them to disk.
417
+ Monitor a website and then if there are any changes,
418
+ log them to disk.
406
419
"""
407
420
while True :
408
421
changes = check_for_changes(some_website)
0 commit comments