I just killed GIL!!!

Nick Craig-Wood nick at craig-wood.com
Fri Apr 18 06:30:03 EDT 2008


Rhamphoryncus <rhamph at gmail.com> wrote:
>  On Apr 17, 7:40 am, Steve Holden <st... at holdenweb.com> wrote:
> > I'd love to be wrong about that, but the GIL *has* been the subject of
> > extensive efforts to kill it over the last five years, and it has
> > survived despite the best efforts of the developers.
> 
>  Yo.  http://code.google.com/p/python-safethread/

Sounds very interesting.  I particularly liked this bit from the web
page - an excellent solution to fine grained locking.  Sending only
immutable objects between threads is very like the functional approach
used by Erlang which is extremely good at concurrency.

------------------------------------------------------------
Which objects can be shared between threads

You probably know how to append to a list or modify a dict. When
confronted with threading though you may start to wonder, what happens
if two threads modify a list or a dict simultaneously? There's two
common answers to this:

    * 1. Corruption. This is the default in C, and to a lesser degree
    Java. It doesn't limit you much and can be faster, but it requires
    some deep voodoo to know when you're using it right. 

    * 2. Locks. All the operations internally lock the object. This
    makes them individually correct, but they'll be wrong again if you
    try to compose them into larger operations. It also adds a
    significant performance penalty. 

My priority is making it easy to write correct programs, and neither
of those options are good enough. Instead, I take a third option as my
default:

    * 3. Make sharing impossible, avoiding the question. list and dict
    cannot be shared between threads. Any attempt to pass a list or
    dict into something like a Queue will raise a TypeError. This
    ensures any thread interaction is explicit, rather than
    implicit. See also: The Zen of Python 

Of course if you couldn't share any objects then you'd just have
processes, which are quite awkward to use. Instead, I only make
mutable objects like list and dict unshareable, while immutable int
and str objects can still be shared. Further, mutable objects that
provide an explicit API for use between threads are also shareable.
------------------------------------------------------------

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list