[Patches] [ python-Patches-450702 ] zlibmodule ALLOW_THREADS update

noreply@sourceforge.net noreply@sourceforge.net
Tue, 14 Aug 2001 12:13:31 -0700


Patches item #450702, was opened at 2001-08-13 22:29
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=450702&group_id=5470

Category: Modules
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Titus Brown (titus)
Assigned to: Nobody/Anonymous (nobody)
Summary: zlibmodule ALLOW_THREADS update

Initial Comment:
When using e.g. the gzip module in a threaded
Python embedding (PyWX, pywx.idyll.org) all other
Python threads halt, because no thread interleaving
is done by the time-intensive commands in zlib.so.
This leads to serious lags when compressing 5 MB files...

I have wrapped all of the inflate and deflate commands
in Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS,
which fixes this problem.  Note that this fix is
backwards compatible to 2.1, at least, as zlibmodule.c
has not changed since then.

As requested, a _context_ diff from the head of the
current CVS tree is attached ;).


----------------------------------------------------------------------

>Comment By: Titus Brown (titus)
Date: 2001-08-14 12:13

Message:
Logged In: YES 
user_id=23486

N.B. I've found that zlib _is_ threadsafe.  I still need to
determine if the way in which objects are passed around in
the zlibmodule.c can potentially cause problems; it seems
like it can.


----------------------------------------------------------------------

Comment By: Titus Brown (titus)
Date: 2001-08-14 09:06

Message:
Logged In: YES 
user_id=23486

I think there may be a 2nd problem: I have to look into
some of the Python calls used to transfer data around, but
it may be possible for threads with access to the 
compression objects to retrieve data in an indeterminate 
state, i.e. mid-compression.

Luckily a module-wide lock should take care of both this 
problem AND resolve threadsafety issues with zlib!

I'll look into this & fix it.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2001-08-14 07:21

Message:
Logged In: YES 
user_id=21627

The patch looks good, however there is a major problem with
the approach taken: zlib itself might not be threadsafe.
Please try to find out whether zlib indeed is thread-safe;
if it isn't, I think you need to add another lock to prevent
multiple Python threads from accessing zlib simultaneously. 

You may want to take a look at _tkinter, which has similar
code to prevent multiple Python threads from accessing Tk
simultaneously.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=305470&aid=450702&group_id=5470