__del__ for cdef classes

33 views
Skip to first unread message

Yury Selivanov

unread,
Nov 25, 2015, 2:14:32 PM11/25/15
to cython-users
Hi,

I'm working on an alternative implementation of asyncio event loop on Cython and libuv: https://github.com/1st1/uvloop

The results so far are extremely promising, I see 2-3.5x performance improvements in benchmarks: https://github.com/python/asyncio/issues/282#issuecomment-155957235

In uvloop I have to carefully track resources of libuv.  It would be nice if I had a way to define __del__ on a cdef class, so that I can resurrect it in some occasions -- I need it to follow PEP 442 semantics.  This way I can implement uvloop to behave closer to asyncio.

Is it possible to add __del__ methods for cdef classes?  Perhaps enabled only with special cython class decorator?  Only for Python > 3.4?

Thanks,
Yury

Hai Nguyen

unread,
Nov 25, 2015, 2:44:06 PM11/25/15
to cython-users
You can try yourself by adding __del__ method. A dummy example here: https://github.com/Amber-MD/pytraj/blob/master/pytraj/frame.pyx#L145-L146

Hai
 
Thanks,
Yury

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Bradshaw

unread,
Nov 25, 2015, 2:47:40 PM11/25/15
to cython...@googlegroups.com
You should look at __dealloc__.

Stefan Behnel

unread,
Nov 25, 2015, 4:06:04 PM11/25/15
to cython...@googlegroups.com
Robert Bradshaw schrieb am 25.11.2015 um 20:47:
> On Wed, Nov 25, 2015 at 9:44 AM, Yury Selivanov wrote:
>> I'm working on an alternative implementation of asyncio event loop on Cython
>> and libuv: https://github.com/1st1/uvloop
>>
>> The results so far are extremely promising, I see 2-3.5x performance
>> improvements in benchmarks:
>> https://github.com/python/asyncio/issues/282#issuecomment-155957235
>>
>> In uvloop I have to carefully track resources of libuv. It would be nice if
>> I had a way to define __del__ on a cdef class, so that I can resurrect it in
>> some occasions -- I need it to follow PEP 442 semantics. This way I can
>> implement uvloop to behave closer to asyncio.
>>
>> Is it possible to add __del__ methods for cdef classes? Perhaps enabled
>> only with special cython class decorator? Only for Python > 3.4?
>
> You should look at __dealloc__.

I don't think __dealloc__ is a good place to try to resurrect an object. It
might already be unusable at that point (GC cycle breaking, subclass
cleanups, ...).

Yury, could you give us an idea of the concrete problem you are trying to
solve? Why would your objects need to be resurrected?

"tp_del" is not currently a supported slot in Cython, but could be added.
See TypeSlots.py.

Stefan

Robert Bradshaw

unread,
Nov 25, 2015, 4:43:45 PM11/25/15
to cython...@googlegroups.com
On Wed, Nov 25, 2015 at 1:06 PM, Stefan Behnel <stef...@behnel.de> wrote:
> Robert Bradshaw schrieb am 25.11.2015 um 20:47:
>> On Wed, Nov 25, 2015 at 9:44 AM, Yury Selivanov wrote:
>>> I'm working on an alternative implementation of asyncio event loop on Cython
>>> and libuv: https://github.com/1st1/uvloop
>>>
>>> The results so far are extremely promising, I see 2-3.5x performance
>>> improvements in benchmarks:
>>> https://github.com/python/asyncio/issues/282#issuecomment-155957235
>>>
>>> In uvloop I have to carefully track resources of libuv. It would be nice if
>>> I had a way to define __del__ on a cdef class, so that I can resurrect it in
>>> some occasions -- I need it to follow PEP 442 semantics. This way I can
>>> implement uvloop to behave closer to asyncio.
>>>
>>> Is it possible to add __del__ methods for cdef classes? Perhaps enabled
>>> only with special cython class decorator? Only for Python > 3.4?
>>
>> You should look at __dealloc__.
>
> I don't think __dealloc__ is a good place to try to resurrect an object. It
> might already be unusable at that point (GC cycle breaking, subclass
> cleanups, ...).

Oh, missed the "resurrect" part.

> Yury, could you give us an idea of the concrete problem you are trying to
> solve? Why would your objects need to be resurrected?

+1

> "tp_del" is not currently a supported slot in Cython, but could be added.
> See TypeSlots.py.
>
> Stefan
>
Reply all
Reply to author
Forward
0 new messages