async_.pyx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. @cython.no_gc_clear
  2. cdef class UVAsync(UVHandle):
  3. cdef _init(self, Loop loop, method_t callback, object ctx):
  4. cdef int err
  5. self._start_init(loop)
  6. self._handle = <uv.uv_handle_t*>PyMem_RawMalloc(sizeof(uv.uv_async_t))
  7. if self._handle is NULL:
  8. self._abort_init()
  9. raise MemoryError()
  10. err = uv.uv_async_init(self._loop.uvloop,
  11. <uv.uv_async_t*>self._handle,
  12. __uvasync_callback)
  13. if err < 0:
  14. self._abort_init()
  15. raise convert_error(err)
  16. self._finish_init()
  17. self.callback = callback
  18. self.ctx = ctx
  19. cdef send(self):
  20. cdef int err
  21. self._ensure_alive()
  22. err = uv.uv_async_send(<uv.uv_async_t*>self._handle)
  23. if err < 0:
  24. exc = convert_error(err)
  25. self._fatal_error(exc, True)
  26. return
  27. @staticmethod
  28. cdef UVAsync new(Loop loop, method_t callback, object ctx):
  29. cdef UVAsync handle
  30. handle = UVAsync.__new__(UVAsync)
  31. handle._init(loop, callback, ctx)
  32. return handle
  33. cdef void __uvasync_callback(
  34. uv.uv_async_t* handle,
  35. ) noexcept with gil:
  36. if __ensure_handle_data(<uv.uv_handle_t*>handle, "UVAsync callback") == 0:
  37. return
  38. cdef:
  39. UVAsync async_ = <UVAsync> handle.data
  40. method_t cb = async_.callback
  41. try:
  42. cb(async_.ctx)
  43. except BaseException as ex:
  44. async_._error(ex, False)