Ticket #13488: 13488.1.diff

File 13488.1.diff, 1.9 KB (added by jbronn, 13 years ago)

Create reference to thread_context in GEOSFunc instances.

  • django/contrib/gis/geos/prototypes/threadsafe.py

    diff -r 7f798c38beea django/contrib/gis/geos/prototypes/threadsafe.py
    a b  
    2020
    2121thread_context = GEOSContext()
    2222
    23 def call_geos_threaded(cfunc, args):
    24     """
    25     This module-level routine calls the specified GEOS C thread-safe
    26     function with the context for this current thread.
    27     """
    28     # If a context handle does not exist for this thread, initialize one.
    29     if not thread_context.handle:
    30         thread_context.handle = GEOSContextHandle()
    31     # Call the threaded GEOS routine with pointer of the context handle
    32     # as the first argument.
    33     return cfunc(thread_context.handle.ptr, *args)
    34 
    3523class GEOSFunc(object):
    3624    """
    3725    Class that serves as a wrapper for GEOS C Functions, and will
     
    4331            # take an additional context handle parameter.
    4432            self.cfunc = getattr(lgeos, func_name + '_r')
    4533            self.threaded = True
     34            # Create a reference here to thread_context so it's not
     35            # garbage-collected before an attempt to call this object.
     36            self.thread_context = thread_context
    4637        except AttributeError:
    4738            # Otherwise, use usual function.
    4839            self.cfunc = getattr(lgeos, func_name)
     
    5041
    5142    def __call__(self, *args):
    5243        if self.threaded:
    53             return call_geos_threaded(self.cfunc, args)
     44            # If a context handle does not exist for this thread, initialize one.
     45            if not self.thread_context.handle:
     46                self.thread_context.handle = GEOSContextHandle()
     47            # Call the threaded GEOS routine with pointer of the context handle
     48            # as the first argument.
     49            return self.cfunc(self.thread_context.handle.ptr, *args)
    5450        else:
    5551            return self.cfunc(*args)
    5652
Back to Top