uselect.rst 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. :mod:`uselect` -- wait for events on a set of streams
  2. ========================================================================
  3. .. module:: uselect
  4. :synopsis: wait for events on a set of streams
  5. |see_cpython_module| :mod:`python:select`.
  6. This module provides functions to efficiently wait for events on multiple
  7. `streams <stream>` (select streams which are ready for operations).
  8. Functions
  9. ---------
  10. .. function:: poll()
  11. Create an instance of the Poll class.
  12. .. function:: select(rlist, wlist, xlist[, timeout])
  13. Wait for activity on a set of objects.
  14. This function is provided by some MicroPython ports for compatibility
  15. and is not efficient. Usage of :class:`Poll` is recommended instead.
  16. .. _class: Poll
  17. class ``Poll``
  18. --------------
  19. Methods
  20. ~~~~~~~
  21. .. method:: poll.register(obj[, eventmask])
  22. Register `stream` *obj* for polling. *eventmask* is logical OR of:
  23. * ``uselect.POLLIN`` - data available for reading
  24. * ``uselect.POLLOUT`` - more data can be written
  25. Note that flags like ``uselect.POLLHUP`` and ``uselect.POLLERR`` are
  26. *not* valid as input eventmask (these are unsolicited events which
  27. will be returned from `poll()` regardless of whether they are asked
  28. for). This semantics is per POSIX.
  29. *eventmask* defaults to ``uselect.POLLIN | uselect.POLLOUT``.
  30. .. method:: poll.unregister(obj)
  31. Unregister *obj* from polling.
  32. .. method:: poll.modify(obj, eventmask)
  33. Modify the *eventmask* for *obj*.
  34. .. method:: poll.poll(timeout=-1)
  35. Wait for at least one of the registered objects to become ready or have an
  36. exceptional condition, with optional timeout in milliseconds (if *timeout*
  37. arg is not specified or -1, there is no timeout).
  38. Returns list of (``obj``, ``event``, ...) tuples. There may be other elements in
  39. tuple, depending on a platform and version, so don't assume that its size is 2.
  40. The ``event`` element specifies which events happened with a stream and
  41. is a combination of ``uselect.POLL*`` constants described above. Note that
  42. flags ``uselect.POLLHUP`` and ``uselect.POLLERR`` can be returned at any time
  43. (even if were not asked for), and must be acted on accordingly (the
  44. corresponding stream unregistered from poll and likely closed), because
  45. otherwise all further invocations of `poll()` may return immediately with
  46. these flags set for this stream again.
  47. In case of timeout, an empty list is returned.
  48. .. admonition:: Difference to CPython
  49. :class: attention
  50. Tuples returned may contain more than 2 elements as described above.
  51. .. method:: poll.ipoll(timeout=-1, flags=0)
  52. Like :meth:`poll.poll`, but instead returns an iterator which yields a
  53. `callee-owned tuple`. This function provides an efficient, allocation-free
  54. way to poll on streams.
  55. If *flags* is 1, one-shot behavior for events is employed: streams for
  56. which events happened will have their event masks automatically reset
  57. (equivalent to ``poll.modify(obj, 0)``), so new events for such a stream
  58. won't be processed until new mask is set with `poll.modify()`. This
  59. behavior is useful for asynchronous I/O schedulers.
  60. .. admonition:: Difference to CPython
  61. :class: attention
  62. This function is a MicroPython extension.