Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6462

Re: GIL in alternative implementations

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '(at': 0.03; '3.2': 0.05; 'cpython': 0.07; 'python': 0.08; 'concurrency': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; '>>>': 0.12; 'def': 0.12; 'wrote:': 0.14; '3.2)': 0.16; 'code...': 0.16; 'containers': 0.16; 'margin.': 0.16; 'pypy': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:GIL': 0.16; 'subject:alternative': 0.16; 't.start()': 0.16; 'threading': 0.16; 'threads:': 0.16; "wouldn't": 0.17; 'java': 0.21; "aren't": 0.22; 'gil': 0.23; 'code': 0.24; 'pass': 0.27; "i'm": 0.27; 'problem': 0.28; '.net': 0.29; 'bugs': 0.29; 'import': 0.29; 'elements': 0.29; 'class': 0.29; 'code,': 0.29; 'least': 0.30; 'from:addr:web.de': 0.30; 'know:': 0.30; 'operation.': 0.30; 'threads': 0.30; 'header:X-Complaints-To:1': 0.32; 'to:addr:python-list': 0.33; "isn't": 0.33; 'thinking': 0.34; 'daniel': 0.34; 'occurs': 0.35; 'typical': 0.35; 'store': 0.35; 'thread': 0.37; 'another': 0.37; 'two': 0.37; 'received:org': 0.38; 'run': 0.38; 'but': 0.38; 'back.': 0.38; 'matters': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'should': 0.39; "i'd": 0.39; 'header:Mime-Version:1': 0.39; 'either': 0.39; 'to:addr:python.org': 0.39; 'getting': 0.40; 'more': 0.60; 'your': 0.60; 'break.': 0.84; 'corrupted': 0.84; 'safe.': 0.95
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: GIL in alternative implementations
Date Sat, 28 May 2011 13:09:44 +0200
Organization None
References <4DE015EA.4040600@gmail.com> <BANLkTikbXA1S2Yw_DkNL-xPeSTVTXAKMFw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849c1f.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2188.1306581000.9059.python-list@python.org> (permalink)
Lines 78
NNTP-Posting-Host 82.94.164.166
X-Trace 1306581000 news.xs4all.nl 49045 [::ffff:82.94.164.166]:37198
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6462

Show key headers only | View raw


Daniel Kluev wrote:

>> So I'd like to know: how do these other implementations handle
>> concurrency matters for their primitive types, and prevent them from
>> getting corrupted in multithreaded programs (if they do) ? I'm not only
>> thinking about python types, but also primitive containers and types used
>> in .Net and Java VMs, which aren't atomic elements either at an
>> assembly-level point of view.
> 
> Well, they definitely have some shortcomings:
> 
> test.py:
> 
> from threading import Thread
> class X(object):
>     pass
> obj = X()
> obj.x = 0
> 
> def f(*args):
>    for i in range(10000):
>        obj.x += 1
> 
> threads = []
> for i in range(100):
>     t = Thread(target=f)
>     threads.append(t)
>     t.start()
> 
> for t in threads:
>     while t.isAlive():
>         t.join(1)
> 
> print(obj.x)
> 
>> python test.py
> 1000000
>> pypy test.py
> 1000000
>> jython-2.5 test.py
> 19217
>> ipy test.py
> 59040
> 
> Not that this thing is reasonable to do in real code, but cpython and
> other implementations with GIL at least give you some safety margin.

The problem with your code is that it gives the wrong result when a thread 
reads obj.x, then suspends, then another thread reads obj.x and finally both 
threads store the same value+1 back. That problem has nothing to do with the 
GIL which just prohibits that two threads can run at the same time, on 
different cores. 

It occurs because obj.x += 1 is not an atomic operation. That lack of 
atomicity should be obvious if you take a look at the byte-code:

>>> def f():
...     obj.x += 1
...
>>> dis.dis(f)
  2           0 LOAD_GLOBAL              0 (obj)
              3 DUP_TOP
              4 LOAD_ATTR                1 (x)
              7 LOAD_CONST               1 (1)
             10 INPLACE_ADD
             11 ROT_TWO
             12 STORE_ATTR               1 (x)
             15 LOAD_CONST               0 (None)
             18 RETURN_VALUE

[Marc Christiansen]
> So even CPython (at least < 3.2) isn't safe. And I wouldn't rely on 3.2
> not to break.

I don't know why it /seems/ to work in 3.2 more often than in 2.x, but in 
general bugs that occur only sporadically are typical for multithreaded 
code...

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: GIL in alternative implementations Peter Otten <__peter__@web.de> - 2011-05-28 13:09 +0200

csiph-web