Python: Thread safe Oject

 
Description:

This is a class I made to make any object thread safe using a simple RLock. It makes the whole thing as transparent as possible, using __getattr__.

 

class  ThreadSafeObject:
    
"""
    A class that makes any object thread safe.
    
"""
    
    
def   __init__ (self, obj):
        
"""
        Initialize the class with the object to make thread safe.
        
"""
        self.lock 
=  threading.RLock()
        self.object 
=  obj
        
    
def   __getattr__ (self, attr):
        self.lock.acquire()
        
def  _proxy( * args,  ** kargs):
            self.lock.acquire()
            answer 
=  getattr(self.object, attr)( * args,  ** kargs)
            self.lock.release()
            
return  answer
        
return  _proxy


Discussion:

I really don't like multi-threading, as it can cause a lot of subtle and hard to recreate bugs, race conditions and deadlocks... So when coworker asked me how to make a list thread safe, I wrote this little hack.

Some issues in this implementation is that the returned object doesn't have the __doc__ and other metainfo of the original class, but that can be added quite easily... I left it out to show the ease and elegance of the implementation... Python is powerful that way.

Sample usage - thread safeing a printer class:

 

class  printer:
def  count(self, name):
for  i  in  range( 50 ):
print  name, i

print   " ***** Not thread safe ***** "
=  printer()
=  threading.Thread(target  =  x.count, args  =  [ " 2nd " ])
z.start()
x.count(
" 1st " )

print   " ***** Thread safe ***** "  
=  ThreadSafeObject(printer())
=  threading.Thread(target  =  x.count, args  =  [ " 2nd " ])
z.run()
x.count(
" 1st " )

 

Output:
***** Not thread safe *****
2nd 0
2nd 1
2nd 2
2nd 3
2nd 4
2nd 5
2nd 6
2nd 7
2nd 8
2nd 9
2nd 10
2nd 11
2nd 12
2nd 13
2nd 14
2nd 15
2nd 16
2nd 17
1st 0
1st 1
1st 2
1st 3
1st 4
1st 5
1st 6
1st 7
1st 8
1st 9
1st 10
1st 11
1st 12
1st 13
1st 14
1st 15
1st 16
1st 17
1st 18
1st 19
1st 20
1st 21
1st 22
1st 23
2nd 18
2nd 19
2nd 20
2nd 21
2nd 22
2nd 23
2nd 24
2nd 25
2nd 26
2nd 27
2nd 28
2nd 29
2nd 30
2nd 31
2nd 32
2nd 33
2nd 34
2nd 35
2nd 36
2nd 37
2nd 38
2nd 39
2nd 40
2nd 41
2nd 42
1st 24
1st 25
1st 26
1st 27
1st 28
1st 29
1st 30
1st 31
1st 32
1st 33
1st 34
1st 35
1st 36
1st 37
1st 38
1st 39
1st 40
1st 41
1st 42
1st 43
1st 44
1st 45
1st 46
1st 47
1st 48
2nd 43
2nd 44
2nd 45
2nd 46
2nd 47
2nd 48
2nd 49
1st 49
***** Thread safe *****
2nd 0
2nd 1
2nd 2
2nd 3
2nd 4
2nd 5
2nd 6
2nd 7
2nd 8
2nd 9
2nd 10
2nd 11
2nd 12
2nd 13
2nd 14
2nd 15
2nd 16
2nd 17
2nd 18
2nd 19
2nd 20
2nd 21
2nd 22
2nd 23
2nd 24
2nd 25
2nd 26
2nd 27
2nd 28
2nd 29
2nd 30
2nd 31
2nd 32
2nd 33
2nd 34
2nd 35
2nd 36
2nd 37
2nd 38
2nd 39
2nd 40
2nd 41
2nd 42
2nd 43
2nd 44
2nd 45
2nd 46
2nd 47
2nd 48
2nd 49
1st 0
1st 1
1st 2
1st 3
1st 4
1st 5
1st 6
1st 7
1st 8
1st 9
1st 10
1st 11
1st 12
1st 13
1st 14
1st 15
1st 16
1st 17
1st 18
1st 19
1st 20
1st 21
1st 22
1st 23
1st 24
1st 25
1st 26
1st 27
1st 28
1st 29
1st 30
1st 31
1st 32
1st 33
1st 34
1st 35
1st 36
1st 37
1st 38
1st 39
1st 40
1st 41
1st 42
1st 43
1st 44
1st 45
1st 46
1st 47
1st 48
1st 49

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值