thread lock question

Ritesh Raj Sarraf riteshsarraf at gmail.com
Thu Oct 12 10:56:04 CEST 2006


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
> <rrs at researchut.com> declaimed 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
> 	wlfraed at ix.netcom.com		wulfraed at bestiaria.com
> 		HTTP://wlfraed.home.netcom.com/
> 	(Bestiaria Support Staff:		web-asst at bestiaria.com)
> 		HTTP://www.bestiaria.com/




More information about the Python-list mailing list