I can't get RLock to work (warning, the following code is prettylong)

G

grocery_stocker

When I run the following...

#!/usr/bin/python

import time
import thread
import threading

def get_first_part(string, lock, sleeptime, *args):
global counter
lock.acquire()
try:
counter = counter + 1
data = counter
print "%s value is %d" % (string, counter)
time.sleep(sleeptime)
finally:
lock.release()
return data

def get_second_part(string, lock, sleeptime, *args):
global counter
lock.acquire()
try:
counter = counter + 1
data = counter
print "%s value is %d" % (string, counter)
time.sleep(sleeptime)
finally:
lock.release()
return data

def get_both_parts(string, lock, sleeptime, *args):
global first, second
lock.acquire()
try:
first = get_first_part()
second = get_second_part()
print "%s values are %d and %d" % (string, first,
second)
time.sleep(sleeptime)
finally:
lock.release()
return first, second

if __name__ == "__main__":
#lock = thread.allocate_lock()
lock = threading.RLock()

counter = 0
first = 0
second = 0

thread.start_new_thread(get_first_part, ("Thread1", lock, 2))
thread.start_new_thread(get_second_part, ("Thread2", lock, 2))
thread.start_new_thread(get_both_parts, ("Thread3", lock, 2))

#Yes I was told this was bad, but I'm still doing until I can make
it beyond
#the basics.
while 1: pass

The code will jsut hang....

[cdalten@localhost oakland]$ ./rlock.py
Thread1 value is 1
Thread2 value is 2
Traceback (most recent call last):
File "./rlock.py", line 57, in ?
while 1: pass
KeyboardInterrupt

How come RLock isn't working in this example?
 
P

Peter Otten

grocery_stocker said:
When I run the following...

#!/usr/bin/python

import time
import thread
import threading

def get_first_part(string, lock, sleeptime, *args):
global counter
lock.acquire()
try:
counter = counter + 1
data = counter
print "%s value is %d" % (string, counter)
time.sleep(sleeptime)
finally:
lock.release()
return data
def get_both_parts(string, lock, sleeptime, *args):
global first, second
lock.acquire()
try:
first = get_first_part()
second = get_second_part()
print "%s values are %d and %d" % (string, first,
second)
time.sleep(sleeptime)
finally:
lock.release()
return first, second
How come RLock isn't working in this example?

When get_both_parts() acquires the lock it invokes get_first_part() which
tries to acquire the lock. This fails because get_both_parts() does not
release the lock until after get_first_part() has finished...

Peter
 
G

grocery_stocker

When get_both_parts() acquires the lock it invokes get_first_part() which
tries to acquire the lock. This fails because get_both_parts() does not
release the lock until after get_first_part() has finished...

Peter


i thought a Rlock() (vs a Lock()) was suspposed to get around this
kind of a problem.
 
P

Peter Otten

grocery_stocker said:
i thought a Rlock() (vs a Lock()) was suspposed to get around this
kind of a problem.

Oops, yes. In get_both_parts() try calling get_first_part/get_second_part()
with the three necessary arguments...

Peter
 
M

Moriaantje

I think it would help if you would call your functions in
get_both_parts with some arguments ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top