Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > I can't get RLock to work (warning, the following code is prettylong)

Reply
Thread Tools

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

 
 
grocery_stocker
Guest
Posts: n/a
 
      04-10-2009
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?
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      04-10-2009
grocery_stocker wrote:

> 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
 
Reply With Quote
 
 
 
 
grocery_stocker
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 6:48 am, Peter Otten <(E-Mail Removed)> wrote:
> grocery_stocker wrote:
> > 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



i thought a Rlock() (vs a Lock()) was suspposed to get around this
kind of a problem.
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      04-10-2009
grocery_stocker wrote:

> On Apr 10, 6:48 am, Peter Otten <(E-Mail Removed)> wrote:
>> grocery_stocker wrote:
>> > 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

>
>
> 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
 
Reply With Quote
 
grocery_stocker
Guest
Posts: n/a
 
      04-10-2009
On Apr 10, 8:39 am, Peter Otten <(E-Mail Removed)> wrote:
> grocery_stocker wrote:
> > On Apr 10, 6:48 am, Peter Otten <(E-Mail Removed)> wrote:
> >> grocery_stocker wrote:
> >> > 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

>
> > 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...
>


Doh. I knew that. I really did.

 
Reply With Quote
 
Moriaantje
Guest
Posts: n/a
 
      04-11-2009
I think it would help if you would call your functions in
get_both_parts with some arguments ...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RLock IO bound? juancarlo.anez@gmail.com Python 1 03-03-2013 06:27 AM
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
OverflowError in RLock.acquire() David Roberts Python 0 07-24-2009 04:27 AM
Why doesn't StopIteration get caught in the following code? grocery_stocker Python 2 04-05-2009 02:48 AM
RE: The Web server reported the following error when attempting to create or open the Web project located at the following URL: <URL> =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?= ASP .Net 0 06-07-2004 07:36 AM



Advertisments