Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

66 строки
2.2 KiB

  1. cdef class UVRequest:
  2. """A base class for all libuv requests (uv_getaddrinfo_t, etc).
  3. Important: it's a responsibility of the subclass to call the
  4. "on_done" method in the request's callback.
  5. If "on_done" isn't called, the request object will never die.
  6. """
  7. def __cinit__(self, Loop loop, *_):
  8. self.request = NULL
  9. self.loop = loop
  10. self.done = 0
  11. Py_INCREF(self)
  12. cdef on_done(self):
  13. self.done = 1
  14. Py_DECREF(self)
  15. cdef cancel(self):
  16. # Most requests are implemented using a threadpool. It's only
  17. # possible to cancel a request when it's still in a threadpool's
  18. # queue. Once it's started to execute, we have to wait until
  19. # it finishes and calls its callback (and callback *must* call
  20. # UVRequest.on_done).
  21. cdef int err
  22. if self.done == 1:
  23. return
  24. if UVLOOP_DEBUG:
  25. if self.request is NULL:
  26. raise RuntimeError(
  27. '{}.cancel: .request is NULL'.format(
  28. self.__class__.__name__))
  29. if self.request.data is NULL:
  30. raise RuntimeError(
  31. '{}.cancel: .request.data is NULL'.format(
  32. self.__class__.__name__))
  33. if <UVRequest>self.request.data is not self:
  34. raise RuntimeError(
  35. '{}.cancel: .request.data is not UVRequest'.format(
  36. self.__class__.__name__))
  37. # We only can cancel pending requests. Let's try.
  38. err = uv.uv_cancel(self.request)
  39. if err < 0:
  40. if err == uv.UV_EBUSY:
  41. # Can't close the request -- it's executing (see the first
  42. # comment). Loop will have to wait until the callback
  43. # fires.
  44. pass
  45. elif err == uv.UV_EINVAL:
  46. # From libuv docs:
  47. #
  48. # Only cancellation of uv_fs_t, uv_getaddrinfo_t,
  49. # uv_getnameinfo_t and uv_work_t requests is currently
  50. # supported.
  51. return
  52. else:
  53. ex = convert_error(err)
  54. self.loop._handle_exception(ex)