Skip to content

Commit 07cfcb9

Browse files
committed
Added threading.defaultlocal
1 parent 92261b3 commit 07cfcb9

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/bd2k/util/threading.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import absolute_import
2-
import logging
32
import sys
43
import threading
54

5+
66
class BoundedEmptySemaphore( threading._BoundedSemaphore ):
77
"""
88
A bounded semaphore that is initially empty.
@@ -46,3 +46,20 @@ def join( self, *args, **kwargs ):
4646
type, value, traceback = self.exc_info
4747
self.exc_info = None
4848
raise type, value, traceback
49+
50+
51+
# noinspection PyPep8Naming
52+
class defaultlocal( threading.local ):
53+
"""
54+
Thread local storage with default values for each field in each thread
55+
56+
>>> l = defaultlocal( foo=42 )
57+
>>> def f(): print l.foo
58+
>>> t = threading.Thread(target=f)
59+
>>> t.start() ; t.join()
60+
42
61+
"""
62+
63+
def __init__( self, **kwargs ):
64+
super( defaultlocal, self ).__init__( )
65+
self.__dict__.update( kwargs )

0 commit comments

Comments
 (0)