Thread Question

Bryan Olson fakeaddress at nowhere.org
Sat Aug 5 14:26:33 EDT 2006


Ritesh Raj Sarraf wrote:
[...]
> I noticed that even though while one thread acquires the lock, the other threads
> don't respect the lock. In fact they just go ahead and execute the statements
> within the lock acquire statement. With this behavior, I'm ending up having a
> partially corrupted zip archive file.

No, Carl's code was fine. I just found his explanation
misleading.


> def run(request, response, func=copy_first_match):
>             '''Get items from the request Queue, process them
>             with func(), put the results along with the
>             Thread's name into the response Queue.
>             
>             Stop running once an item is None.'''
>         
>             name = threading.currentThread().getName()
>             ziplock = threading.Lock()

You don't want "ziplock = threading.Lock()" in the body of
the function. It creates a new and different lock on every
execution. Your threads are all acquiring different locks.
To coordinate your threads, they need to be using the same
lock.

Try moving "ziplock = threading.Lock()" out of the function, so
your code might read, in part:


    ziplock = threading.Lock()

    def run(request, response, func=copy_first_match):
        # And so on...


-- 
--Bryan



More information about the Python-list mailing list