[pypy-dev] Thinking about the GIL

Timothy Baldridge tbaldridge at gmail.com
Wed Mar 16 18:51:37 CET 2011

> If the data is constant you can share it in Erlang by generating a
> module for it and putting it in the code server (a hack, but it works)
> or simply using a binary. These are zero-copy in Erlang.
> -bob

True, but that doesn't really solve the problem of writes; you can't
have 5 threads hammering the same data structure. In essence this
model doesn't really solve any more of the same basic issues that
multiprocessing does. In the erlang model you can have thousands of
"threads", and currently with multiprocessing you're limited to <
100-ish. But they still both require you to pass messages.

We should probably mention a somewhat new method of co-currency. This
is my favorite, and is used by Clojure. First of all, I must preface
this with the statement that almost all structures in Clojure are

d = {}
d = d.assoc(key, value)

l = []
l = l.append("foo")

So basically every time you add or remove something from a collection
it returns a new collection. Behind the scenes the collections
carefully manipulate the structures as to a) not modify the old
structure and b) maintain reasonable performance (adding a item to a
hashset does not copy the entire hashset).

That alone gets rid of 90% of your co-currency issues. From there you
have refs for Software Transactional Memory. Here's an example of how
it would be used in a Python-esque syntax:

val = 0
account1 = Ref(100)
account2 = Ref(0)

   account1.value = account1.value - 50
   account2.value = account2.value + 50

The transaction block insures that either the entire block succeeds,
or fails as an atomic unit.

Now the nice thing is, most of this can be done without any special
syntax. It's possible to use the Clojure-CLR STM libs within C#, and I
have, and it works like a dream. It's a tad ugly, but it works. Adding
a bit of syntax sugar to python (like the transaction block) should
make this quite elegant.

At any rate, here's a video from Rich Hickey the creator of Clojure on
how modern lock based systems and even perhaps the Erlang model is
quite flawed: http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey

Just some more points to think over,


More information about the Pypy-dev mailing list