473,503 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
1 1048
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1311
by: fowlertrainer | last post by:
Hello ! I have been created a Zope (Python) Product. But it is must store inner global definitions (some conversion parameters), what I need to change in only one thread, or only once. See...
31
2434
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
5
2591
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
27
1703
by: Ritesh Raj Sarraf | last post by:
Hi, I have some basic doubts about thread. I have a list which has items in it which need to be downloaded from the internet. Let's say list is: list_items which has 100 items in it.
6
5804
by: Extremest | last post by:
I am new to threading and trying to figure some things out. Are all variables in a thread set to only that thread? Meaning if I create 2 instances of a class and then put each one in a different...
5
3778
by: admin | last post by:
ok This is my main. Pretty much it goes through each category and starts up 4 worker threads that then ask for groups to gether from. My problem is that when the thread gets done it keeps the...
6
3125
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
2
5363
by: digz | last post by:
Hi, I am trying to write a program which has two threads one of them write to a map , and the other one deletes entries based on a certain criterion.. first I cannot get the delete portion to...
13
3564
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
3
3061
by: andreas.zetterstrom | last post by:
I'm implementing some different c++ classes which I want to be thread safe. All these classes contain lists or arrays of some kind. I'm using protected sections to make them thread safe. The...
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.