a basic bytecode to machine code compiler

Chris Colbert sccolbert at gmail.com
Sat Apr 2 23:25:30 EDT 2011


>>> That's quite an interesting idea. I do think a lot of production Python
> code implicitly depends on the GIL and would need rework for multicore.
> For example, code that expects "n += 1" to be atomic, because the
> CPython bytecode interpreter won't switch threads in the middle of it.
> --
>
Yes it will. The interpreter may switch threads between any of these
bytecode instructions.
In [8]: dis.dis(compile('n += 1', '', 'single'))
 1 0 LOAD_NAME 0 (n)
 3 LOAD_CONST 0 (1)
 6 INPLACE_ADD
 7 STORE_NAME 0 (n)
 10 LOAD_CONST 1 (None)
 13 RETURN_VALUE
Proof:
In [15]: n = 0
In [16]: class Foo(threading.Thread):
 ....: def run(self):
 ....: global n
 ....: for i in range(10000):
 ....: n += 1
 ....:
 ....:
In [17]: threads = [Foo() for i in range(5)]
In [18]: for thread in threads:
 ....: thread.start()
 ....:
 ....:
In [19]: n
Out[19]: 37433
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110402/4ce882c9/attachment-0001.html>


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /