You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

python-api.md 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Python API
  2. ## Operator
  3. The operator API is a framework for you to implement. The implemented operator will be managed by `dora`. This framework enable us to make optimisation and provide advanced features. It is the recommended way of using `dora`.
  4. An operator requires an `on_input` method and requires to return a `DoraStatus` of 0 or 1, depending of it needs to continue or stop.
  5. ```python
  6. class Operator:
  7. def on_input(
  8. self,
  9. input_id: str,
  10. value: bytes,
  11. send_output: Callable[[str, bytes], None],
  12. ) -> DoraStatus:
  13. ```
  14. > For Python, we recommend to allocate the operator on a single runtime. A runtime will share the same GIL with several operators making those operators run almost sequentially. See: [https://docs.rs/pyo3/latest/pyo3/marker/struct.Python.html#deadlocks](https://docs.rs/pyo3/latest/pyo3/marker/struct.Python.html#deadlocks)
  15. ### Try it out!
  16. - Create an operator python file called `op.py`:
  17. ```python
  18. {{#include ../../examples/python-operator/op.py}}
  19. ```
  20. - Link it in your graph as:
  21. ```yaml
  22. {{#include ../../binaries/coordinator/examples/graphs/mini-dataflow.yml:67:73}}
  23. ```
  24. ## Custom Node
  25. The custom node API allow you to integrate `dora` into your application. It allows you to retrieve input and send output in any fashion you want.
  26. #### `Node()`
  27. `Node()` initiate a node from environment variables set by `dora-coordinator`
  28. ```python
  29. from dora import Node()
  30. node = Node()
  31. ```
  32. #### `.next()` or `__next__()` as an iterator
  33. `.next()` gives you the next input that the node has received. It blocks until the next input becomes available. It will return `None` when all senders has been dropped.
  34. ```python
  35. input_id, value = node.next()
  36. # or
  37. for input_id, value in node:
  38. ```
  39. #### `.send_output(output_id, data)`
  40. `send_output` send data from the node.
  41. ```python
  42. node.send_output("string", b"string")
  43. ```
  44. ### Try it out!
  45. - Install python node API:
  46. ```bash
  47. cd apis/python/node
  48. python3 -m venv .env
  49. source .env/bin/activate
  50. pip install maturin
  51. maturin develop
  52. ```
  53. - Create a python file called `printer.py`:
  54. ```python
  55. {{#include ../../binaries/coordinator/examples/nodes/python/printer.py}}
  56. ```
  57. - Link it in your graph as:
  58. ```yaml
  59. {{#include ../../binaries/coordinator/examples/graphs/python_test.yml:12:17}}
  60. ```

DORA (Dataflow-Oriented Robotic Architecture) is middleware designed to streamline and simplify the creation of AI-based robotic applications. It offers low latency, composable, and distributed datafl