Prev: Football was: Python dynamic attribute creation
Next: [python] how to ensure item in list or dict bind with "an uuid meaning" integer type ID?
From: sturlamolden on 28 Jun 2010 23:32 On 29 Jun, 05:11, Ryan Kelly <r...(a)rfk.id.au> wrote: > Very interesting idea. Will it work if accessed through ctypes? > > ticker = ctypes.c_int.in_dll(ctypes.pythonapi,"_Py_Ticker") > ticker.value = 0x7fffffff > > Or does ctypes muck with the GIL in a way that would break this idea? > >>> ctypes.pythonapi <PyDLL 'python dll', handle 1e000000 at 1ffead0> It's a PyDLL, so it should not mock with the GIL. from contextlib import contextmanager import ctypes _Py_Ticker = ctypes.c_int.in_dll(ctypes.pythonapi,"_Py_Ticker") @contextmanager def atomic(): tmp = _Py_Ticker.value _Py_Ticker.value = 0x7fffffff yield _Py_Ticker.value = tmp - 1 Now we can do with atomic(): # whatever pass Just make sure we don't call extension code that releases the GIL :)
From: Antoine Pitrou on 2 Jul 2010 16:26
On Mon, 28 Jun 2010 16:46:41 -0700 Zac Burns <zac256(a)gmail.com> wrote: > In my experience it is far more expensive to allocate a lock in python then > it is the types that use them. Here are some examples: > > >>> timeit.timeit('Lock()', 'from threading import Lock') > 1.4449114807669048 > > >>> timeit.timeit('dict()') > 0.2821554294221187 > > >>> timeit.timeit('list()') > 0.17358153222312467 I'm not sure what Python version on what machine you are using, but here (Python 2.7): >>> timeit.timeit('Lock()', 'from threading import Lock') 0.09944796562194824 >>> timeit.timeit('dict()') 0.17817902565002441 >>> timeit.timeit('list()') 0.19633007049560547 >>> timeit.timeit('{}') 0.03823709487915039 >>> timeit.timeit('[]') 0.05156302452087402 |