By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

thread lock question

P: n/a
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"

Oct 11 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Dennis Lee Bieber wrote:
On Wed, 11 Oct 2006 18:39:32 +0530, Ritesh Raj Sarraf
<rr*@researchut.comdeclaimed the following in comp.lang.python:

if download_file(foo, 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(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
wl*****@ix.netcom.com wu******@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: we******@bestiaria.com)
HTTP://www.bestiaria.com/
Oct 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.