2

It is essentially an extension of this question - Usage of threadpoolexecutor in conjunction with cython's nogil

In this case my getArea2() method is slightly different

cdef int getArea2(self,double[:] p) nogil:
   cdef int area
   cdef SphericalPoint spoint
   spoint = SphericalPoint()
   area = 0
   area = self.rect.getArea()
   return area

and the .pxd declaration is slightly different

cdef extern from "Point.h":


cdef cppclass SphericalPoint(Point):
   SphericalPoint() except +
   double getCoordinate1()
   double getCoordinate2()
   void setCoordinate1(double lat)
   void setCoordinate2(double lon)

With this I am getting these compilation errors. Any reason why the instantiation of the object is incorrect ?

I tried making spoint as a pointer and instantiating the object and it gives me the same error

   cdef SphericalPoint *spoint
   spoint = new SphericalPoint()

Error compiling Cython file:

cdef int getArea2(self,double[:] p) nogil:
   cdef int area
   cdef SphericalPoint spoint
   spoint = SphericalPoint()
                         ^
  ------------------------------------------------------------

 test.pyx:78:30: Calling gil-requiring function not allowed without gil
gansub
  • 1,164
  • 3
  • 20
  • 47
  • I think the problem is `except+` but I don't know what you can do about it. The conversation if C++ to Python exceptions will definitely require the GIL, but equally not handling them will automatically crash your whole program – DavidW Jul 29 '19 at 06:23
  • @DavidW Thanks for your response as always. Do you mean the **except +** on SphericalPoint in the .pxd file? I removed it and I get the same error – gansub Jul 29 '19 at 06:28
  • I'll have a proper look later. My thinking was that the `execpt +` was both the problem and necessary (so simply removing it wasn't the solution). However it sounds like I'm wrong (removing it would have fixed the immediate issue but given you other issues) – DavidW Jul 29 '19 at 06:35

1 Answers1

4

It's a really simple issue: Cython assumes that any function needs the GIL unless you tell it otherwise. In the pxd declaration:

SphericalPoint() nogil except +

You probably want to add nogil to the other member functions too.


I initially thought the problem was the except + annotation. This causes Cython to catch any C++ exceptions and translate them to Python exceptions. This does require the GIL, but only if an exception is caught, and Cython takes care of this automatically, so don't worry about it. The alternative is the whole program crashing due to unhandled C++ exceptions so you do need to keep the except +.

DavidW
  • 29,336
  • 6
  • 55
  • 86