Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11480b6

Browse files
committed
Mention concurrent.futures and update answers about the GIL.
1 parent f700524 commit 11480b6

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

Doc/faq/library.rst

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,15 @@ queue as there are threads.
285285
How do I parcel out work among a bunch of worker threads?
286286
---------------------------------------------------------
287287

288-
Use the :mod:`queue` module to create a queue containing a list of jobs. The
289-
:class:`~queue.Queue` class maintains a list of objects with ``.put(obj)`` to
290-
add an item to the queue and ``.get()`` to return an item. The class will take
291-
care of the locking necessary to ensure that each job is handed out exactly
292-
once.
288+
The easiest way is to use the new :mod:`concurrent.futures` module,
289+
especially the :mod:`~concurrent.futures.ThreadPoolExecutor` class.
290+
291+
Or, if you want fine control over the dispatching algorithm, you can write
292+
your own logic manually. Use the :mod:`queue` module to create a queue
293+
containing a list of jobs. The :class:`~queue.Queue` class maintains a
294+
list of objects with ``.put(obj)`` to add an item to the queue and ``.get()``
295+
to return an item. The class will take care of the locking necessary to
296+
ensure that each job is handed out exactly once.
293297

294298
Here's a trivial example::
295299

@@ -352,7 +356,7 @@ provides a featureful interface.
352356
What kinds of global value mutation are thread-safe?
353357
----------------------------------------------------
354358

355-
A global interpreter lock (GIL) is used internally to ensure that only one
359+
A :term:`global interpreter lock` (GIL) is used internally to ensure that only one
356360
thread runs in the Python VM at a time. In general, Python offers to switch
357361
among threads only between bytecode instructions; how frequently it switches can
358362
be set via :func:`sys.setswitchinterval`. Each bytecode instruction and
@@ -395,32 +399,34 @@ lists. When in doubt, use a mutex!
395399
Can't we get rid of the Global Interpreter Lock?
396400
------------------------------------------------
397401

398-
.. XXX mention multiprocessing
399402
.. XXX link to dbeazley's talk about GIL?
400403
401-
The Global Interpreter Lock (GIL) is often seen as a hindrance to Python's
404+
The :term:`global interpreter lock` (GIL) is often seen as a hindrance to Python's
402405
deployment on high-end multiprocessor server machines, because a multi-threaded
403406
Python program effectively only uses one CPU, due to the insistence that
404407
(almost) all Python code can only run while the GIL is held.
405408

406409
Back in the days of Python 1.5, Greg Stein actually implemented a comprehensive
407410
patch set (the "free threading" patches) that removed the GIL and replaced it
408-
with fine-grained locking. Unfortunately, even on Windows (where locks are very
409-
efficient) this ran ordinary Python code about twice as slow as the interpreter
410-
using the GIL. On Linux the performance loss was even worse because pthread
411-
locks aren't as efficient.
412-
413-
Since then, the idea of getting rid of the GIL has occasionally come up but
414-
nobody has found a way to deal with the expected slowdown, and users who don't
415-
use threads would not be happy if their code ran at half at the speed. Greg's
416-
free threading patch set has not been kept up-to-date for later Python versions.
411+
with fine-grained locking. Adam Olsen recently did a similar experiment
412+
in his `python-safethread <http://code.google.com/p/python-safethread/>`_
413+
project. Unfortunately, both experiments exhibited a sharp drop in single-thread
414+
performance (at least 30% slower), due to the amount of fine-grained locking
415+
necessary to compensate for the removal of the GIL.
417416

418417
This doesn't mean that you can't make good use of Python on multi-CPU machines!
419418
You just have to be creative with dividing the work up between multiple
420-
*processes* rather than multiple *threads*. Judicious use of C extensions will
421-
also help; if you use a C extension to perform a time-consuming task, the
422-
extension can release the GIL while the thread of execution is in the C code and
423-
allow other threads to get some work done.
419+
*processes* rather than multiple *threads*. The
420+
:class:`~concurrent.futures.ProcessPoolExecutor` class in the new
421+
:mod:`concurrent.futures` module provides an easy way of doing so; the
422+
:mod:`multiprocessing` module provides a lower-level API in case you want
423+
more control over dispatching of tasks.
424+
425+
Judicious use of C extensions will also help; if you use a C extension to
426+
perform a time-consuming task, the extension can release the GIL while the
427+
thread of execution is in the C code and allow other threads to get some work
428+
done. Some standard library modules such as :mod:`zlib` and :mod:`hashlib`
429+
already do this.
424430

425431
It has been suggested that the GIL should be a per-interpreter-state lock rather
426432
than truly global; interpreters then wouldn't be able to share objects.

0 commit comments

Comments
 (0)