thread lock question

R

Ritesh Raj Sarraf

Hi,

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

if download_file(foo, bar) == True:
if some condition:
do_something()
if zip_bool:
ziplock.acquire()
try:
compress_the_file(zip_type_file, foo, bar)
os.unlink(foo) # Unlink it once it has been zipped
finally:
ziplock.release()
if zip_bool:
ziplock.acquire()
try:
compress_the_file(zip_type_file, 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"
 
D

Dennis Lee Bieber

if download_file(foo, bar) == True:
if some condition:
do_something()
if zip_bool:
said:
if zip_bool:
said:
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(foo, 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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
R

Ritesh Raj Sarraf

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

exit_status = copy_first_match(foo, bar)

if exit_status == False:
if download_file(foo, bar) == True:
if zip_bool:
ziplock.acquire()
try:
compress_the_file(zip_type_file, foo,
bar)
os.unlink(foo) # Unlink it once it has
been zipped
finally:
ziplock.release()
else:
if zip_bool:
ziplock.acquire()
try:
compress_the_file(zip_type_file, 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
 
D

Dennis Lee Bieber

exit_status = copy_first_match(foo, bar)

if exit_status == False:
if download_file(foo, bar) == True:
if zip_bool:
ziplock.acquire()
try:
compress_the_file(zip_type_file, foo,
bar)
os.unlink(foo) # Unlink it once it has
been zipped
finally:
ziplock.release()
else:
if zip_bool:
ziplock.acquire()
try:
compress_the_file(zip_type_file, foo, bar)
os.unlink(foo)
finally:
ziplock.release()

I think this code should be clear enough.

And still rather redundant, though you have removed the misleading
"repeat" of the zip operation.

As I understand the above, the only way to NOT perform a zip,
requires the file to NOT be cached, AND the download has to fail.

-=-=-=-=-=-=-
def prepareFile(foo, bar):
if not copy_first_match(foo, bar):
return download_file(foo, bar)
else:
return True

def compressFile(ztfile, foo, bar):
ziplock.acquire()
try:
compress_the_file(ztfile, foo, bar)
os.unlink(foo)
finally:
#ignores all error conditions -- such as failure in
#compress, or failure to delete the file
ziplock.release()
-=-=-=-=-=-=-
....

if prepareFile(foo, bar):
if zip_bool:
compressFile(ztfile, foo, bar)

....
-=-=-=-=-=-=-
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 ?
Since you don't show the structure of the code that creates the lock
object, I can not report on all your conditions.

Presuming you only create ONE lock object (ziplock =
threading.Lock(), say) which is shared by all threads, then that is the
only lock, and only one thread of control can acquire it at a time. But
if, by some mischance, you create a lock object as part of the start-up
of each thread, then NO thread will EVER wait (in effect, you created a
new door for each thread, and only that thread ever goes through that
door).
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top