thread lock question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ritesh Raj Sarraf

    #1

    thread lock question

    Hi,

    I have a question regarding locks in threads.
    Following is the code example:

    if download_file(f oo, bar) == True:
    if some condition:
    do_something()
    if zip_bool:
    ziplock.acquire ()
    try:
    compress_the_fi le(zip_type_fil e, foo, bar)
    os.unlink(foo) # Unlink it once it has been zipped
    finally:
    ziplock.release ()
    if zip_bool:
    ziplock.acquire ()
    try:
    compress_the_fi le(zip_type_fil e, foo, bar)
    os.unlink(foo)
    finally:
    ziplock.release ()

    Basically, I'm testing for some condition, if that is True, do something and
    then do the zipping. I'm also doing the zipping even if that's not true.

    My question is, in a code example like this which is threaded, does the locking
    mechanism work correctly ?
    Or are two different locks being acquired ?

    Thanks,
    Ritesh
    --
    Ritesh Raj Sarraf
    RESEARCHUT - http://www.researchut.com
    "Necessity is the mother of invention."
    "Stealing logic from one person is plagiarism, stealing from many is research."
    "The great are those who achieve the impossible, the petty are those who
    cannot - rrs"

  • Ritesh Raj Sarraf

    #2
    Re: thread lock question

    I'm sorry. My example code wasn't clear enough.
    Please see the following:

    exit_status = copy_first_matc h(foo, bar)

    if exit_status == False:
    if download_file(f oo, bar) == True:
    if zip_bool:
    ziplock.acquire ()
    try:
    compress_the_fi le(zip_type_fil e, foo,
    bar)
    os.unlink(foo) # Unlink it once it has
    been zipped
    finally:
    ziplock.release ()
    else:
    if zip_bool:
    ziplock.acquire ()
    try:
    compress_the_fi le(zip_type_fil e, foo, bar)
    os.unlink(foo)
    finally:
    ziplock.release ()

    I think this code should be clear enough.
    The program, before trying to download from the web, checks into its
    cache repository to see if the file is available or not. If available,
    it copies it from the local cache repository and adds to the archive,
    else it downloads from the web and archives it.

    Now in this scenario, with threads, say a file of 100 mb got downloaded
    and then being zipped. It might take a couple of seconds for the zip to
    complete. During those couple of seconds, if another thread's file (a
    couple kb) gets downloaded/copied, will it wait for the lock to be
    released or will it create another lock ?

    Thanks,
    Ritesh


    Dennis Lee Bieber wrote:
    On Wed, 11 Oct 2006 18:39:32 +0530, Ritesh Raj Sarraf
    <rrs@researchut .comdeclaimed the following in comp.lang.pytho n:
    >

    if download_file(f oo, bar) == True:
    if some condition:
    do_something()
    if zip_bool:
    <snip>
    if zip_bool:
    <snip>
    >
    Basically, I'm testing for some condition, if that is True, do something and
    then do the zipping. I'm also doing the zipping even if that's not true.
    Actually, given the code you show, if "some condition" is true, you
    are /attempting/ to zip the file twice; the second one likely fails due
    to the first one deleting the file -- a condition masked by the finally:
    clause.
    >
    All you really need there is
    >
    if download_file(f oo, bar):
    if some condition:
    do_something()
    if zip_bool:
    ....
    >
    >
    My question is, in a code example like this which is threaded, does the locking
    mechanism work correctly ?
    Or are two different locks being acquired ?
    >
    Is the lock object itself defined globally (shared by the threads)?
    --
    Wulfraed Dennis Lee Bieber KD6MOG
    wlfraed@ix.netc om.com wulfraed@bestia ria.com

    (Bestiaria Support Staff: web-asst@bestiaria. com)
    HTTP://www.bestiaria.com/

    Comment

    Working...