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

Discussion in 'Python' started by grocery_stocker, Apr 10, 2009.

  1. 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?
     
    grocery_stocker, Apr 10, 2009
    #1
    1. Advertising

  2. grocery_stocker

    Peter Otten Guest

    Re: I can't get RLock to work (warning, the following code is pretty long)

    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
     
    Peter Otten, Apr 10, 2009
    #2
    1. Advertising

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

    On Apr 10, 6:48 am, Peter Otten <> 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.
     
    grocery_stocker, Apr 10, 2009
    #3
  4. grocery_stocker

    Peter Otten Guest

    Re: I can't get RLock to work (warning, the following code is pretty long)

    grocery_stocker wrote:

    > On Apr 10, 6:48 am, Peter Otten <> 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
     
    Peter Otten, Apr 10, 2009
    #4
  5. Re: I can't get RLock to work (warning, the following code is prettylong)

    On Apr 10, 8:39 am, Peter Otten <> wrote:
    > grocery_stocker wrote:
    > > On Apr 10, 6:48 am, Peter Otten <> 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.
     
    grocery_stocker, Apr 10, 2009
    #5
  6. grocery_stocker

    Moriaantje Guest

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

    I think it would help if you would call your functions in
    get_both_parts with some arguments ...
     
    Moriaantje, Apr 11, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Becker
    Replies:
    0
    Views:
    1,405
    Pete Becker
    Feb 10, 2005
  2. David Roberts

    OverflowError in RLock.acquire()

    David Roberts, Jul 24, 2009, in forum: Python
    Replies:
    0
    Views:
    298
    David Roberts
    Jul 24, 2009
  3. Xeno Campanoli
    Replies:
    1
    Views:
    364
    James Britt
    Jul 1, 2005
  4. nick
    Replies:
    3
    Views:
    412
    Grant Wagner
    Jun 15, 2004
  5. RLock IO bound?

    , Mar 3, 2013, in forum: Python
    Replies:
    1
    Views:
    106
    Cameron Simpson
    Mar 3, 2013
Loading...

Share This Page