467,915 Members | 1,267 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

thread lock question

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
  • viewed: 936
Share:
1 Reply
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.

Similar topics

reply views Thread by fowlertrainer | last post: by
31 posts views Thread by AlexeiOst | last post: by
27 posts views Thread by Ritesh Raj Sarraf | last post: by
6 posts views Thread by Extremest | last post: by
6 posts views Thread by fniles | last post: by
2 posts views Thread by digz | last post: by
13 posts views Thread by arun.darra | last post: by
3 posts views Thread by andreas.zetterstrom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.