Reputation: 797
I am new to python and trying to understand the concept of threading.Lock. Below is the example I typed in,
#!/usr/bin/python
import threading
import Queue
class suleman(threading.Thread):
def __init__(self,q,lock):
threading.Thread.__init__(self)
self.lock=lock
self.queue=q
def run(self):
self.lock.acquire()
file=open('sul.txt','a')
q=self.queue.get()
print q
q=str(q)
file.write(q)
self.lock.release()
self.queue.task_done()
queue=Queue.Queue()
lock=threading.Lock
for i in range(0,10):
z1=suleman(queue,lock)
z2=suleman(queue,lock)
z1.setDaemon(True)
z2.setDaemon(True)
z1.start()
z2.start()
for i in range(0,10):
queue.put(i)
queue.join()
Its giving the following error:
File "lock.py", line 11, in run
self.lock.acquire()
AttributeError: 'builtin_function_or_method' object has no attribute 'acquire'
Any help would be highly appreciated!
Upvotes: 2
Views: 5565
Reputation: 3521
Threading.Lock is not a lock, it creates a lock:
In [1]: import threading
In [2]: threading.Lock
Out[2]: <function thread.allocate_lock>
In [3]: threading.Lock()
Out[3]: <thread.lock at 0x7f9ea666def0>
So you need to change lock=threading.Lock
to lock=threading.Lock()
.
Upvotes: 4