locks

Jeremy Jones zanesdad at bellsouth.net
Wed Oct 13 09:35:06 EDT 2004


Peter L Hansen wrote:
<snip>

> Cliff, do you have any references, or even personal experience to
> relate about anything on which you comment above?
>
> In my experience, and to my knowledge, Python threading *is*
> that easy (ignoring higher level issues such as race conditions
> and deadlocks and such), and the GIL *does* do exactly what Diez
> suggests, and you will *not* get tracebacks nor (again, ignoring
> higher level issues) mangled data.
>
> You've tentatively upset my entire picture of the CPython (note,
> CPython only) interpreter's structure and concept.  Please tell
> me you were going a little overboard to protect a possible
> newbie from himself or something.
>
> -Peter

I seem to remember a posting a while ago that talked about certain 
"activities" generating more than one bytecode operation and were 
therefore not threadsafe.  Maybe I'm remembering wrong.  I can't find 
the posting and I can't remember what data types it was talking about.  
I found this old one about += not being thread safe because it generates 
more than one bytecode:

http://groups.google.com/groups?hl=en&lr=&c2coff=1&threadm=3adn6.3087%24TW.15865%40tor-nn1.netcom.ca&rnum=3&prev=/groups%3Fq%3Dpython%2Bbytecode%2Bthread%26hl%3Den%26lr%3D%26c2coff%3D1%26selm%3D3adn6.3087%2524TW.15865%2540tor-nn1.netcom.ca%26rnum%3D3

Here's a plus equal function def with its respective bytecode operations:

In [33]: def plus_eq(num):
   ....:     num += 1
   ....:     return num
   ....:

In [34]: dis.dis(plus_eq)
  2           0 LOAD_FAST                0 (num)
              3 LOAD_CONST               1 (1)
              6 INPLACE_ADD
              7 STORE_FAST               0 (num)

  3          10 LOAD_FAST                0 (num)
             13 RETURN_VALUE
             14 LOAD_CONST               0 (None)
             17 RETURN_VALUE


Looks like there is an add then a store, so there is a chance for 
multiple threads to mangle this one.


Here's the definition of an append function with its respective bytecode 
operations:

In [35]: def list_append(l):
   ....:     l.append(1)
   ....:


In [38]: dis.dis(list_append)
  2           0 LOAD_FAST                0 (l)
              3 LOAD_ATTR                1 (append)
              6 LOAD_CONST               1 (1)
              9 CALL_FUNCTION            1
             12 POP_TOP
             13 LOAD_CONST               0 (None)
             16 RETURN_VALUE


I don't know enough about the bytecode operations to tell exactly what 
is going on.  I guess if it's doing the append in one bytecode 
operation, it's threadsafe.  If someone could chime in who can read the 
runes of bytecode ops, that could settle the matter.

Jeremy Jones

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041013/9a37c3d4/attachment.html>


More information about the Python-list mailing list