[Assorted-commits] SF.net SVN: assorted:[1869] sandbox/trunk/src/py/threadlocalgc.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2013-08-26 06:57:30
|
Revision: 1869 http://sourceforge.net/p/assorted/svn/1869 Author: yangzhang Date: 2013-08-26 06:57:29 +0000 (Mon, 26 Aug 2013) Log Message: ----------- Add Python thread-local GC demo Added Paths: ----------- sandbox/trunk/src/py/threadlocalgc.py Added: sandbox/trunk/src/py/threadlocalgc.py =================================================================== --- sandbox/trunk/src/py/threadlocalgc.py (rev 0) +++ sandbox/trunk/src/py/threadlocalgc.py 2013-08-26 06:57:29 UTC (rev 1869) @@ -0,0 +1,26 @@ +# From <http://stackoverflow.com/questions/1478248/whats-the-life-time-of-a-thread-local-value-in-python> + +import time +import threading +import gc + +data = threading.local() + +class Resource(object): + def __init__(self): + self.name = threading.currentThread().name + print 'create: %s' % self.name + + def __del__(self): + print 'delete: %s' % self.name + +def access_thlocal(): + data.key = Resource() + +for i in range(0, 10): + threading.Thread(target=access_thlocal).start() +time.sleep(1) +print "Triggering GC" +gc.collect() +time.sleep(1) + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |