Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attribute error in python Locks

I was experimenting with python locks and trying to understand them. I have three classes as follows

LockMain.py

import time
from lock1 import *
from lock2 import *
import threading

class LockMain(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.lock = threading.Lock
        self.obj1 = Lock1(self,"obj1")
        self.obj2 = Lock2(self,"obj2")
        threading.Thread(target=self.obj1.run).start()
        threading.Thread(target=self.obj2.run).start()

    def method1(self,str):
        with self.lock:
            print str+"Method1 entered"
            time.sleep(5)

    def method2(self,str):
        with self.lock:
            print str+"Method2 entered"
            time.sleep(5)

if __name__ == "__main__":
    obj = LockMain()

Lock1.py

import threading
import time

class Lock1(threading.Thread):
    def __init__(self,obj,str):
        threading.Thread.__init__(self)
        self.obj = obj
        self.str = str

    def run(self):
        count = 0
        while True:
            count += 1
            print self.str+str(count)
            time.sleep(1)
            if count == 20:
                print self.str+" entering method 1"
                self.obj.method1(self.str)

Lock2.py

import threading
import time

class Lock2(threading.Thread):
    def __init__(self,obj,str):
        threading.Thread.__init__(self)
        self.obj = obj
        self.str = str

    def run(self):
        count = 0
        while(True):
            count += 1
            print self.str+str(count)
            time.sleep(1)
            if count == 20:
                print self.str+" entering method 2"
                self.obj.method2(self.str)

the code runs fine till both the threads try to enter into method1 and method2 respectively I am getting the following error:-

obj1 entering method 1obj2 entering method 2

Exception in thread Thread-4:
Traceback (most recent call last):
  File "C:\Python27x64\lib\threading.py", line 530, in __bootstrap_inner
    self.run()
  File "C:\Python27x64\lib\threading.py", line 483, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\tsingh\Documents\lock1.py", line 18, in run
    self.obj.method1(self.str)
  File "C:/Users/tsingh/Documents/lockmain.py", line 17, in method1
    with self.lock:
AttributeError: __exit__

Exception in thread Thread-5:
Traceback (most recent call last):
  File "C:\Python27x64\lib\threading.py", line 530, in __bootstrap_inner
    self.run()
  File "C:\Python27x64\lib\threading.py", line 483, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\tsingh\Documents\lock2.py", line 18, in run
    self.obj.method2(self.str)
  File "C:/Users/tsingh/Documents/lockmain.py", line 23, in method2
    with self.lock:
AttributeError: __exit__

Can someone please point out what am I doing wrong here?

like image 726
Tilak Raj Singh Avatar asked Sep 17 '15 22:09

Tilak Raj Singh


2 Answers

You forgot to instantiate the threading.Lock class.

Simply write self.lock = threading.Lock() instead of self.lock = threading.Lock in your LockMain.__init__()

like image 113
cg909 Avatar answered Sep 22 '22 00:09

cg909


Try, in your init: self.lock = threading.Lock()

like image 45
DevLounge Avatar answered Sep 20 '22 00:09

DevLounge