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.2 kB

3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_event` method and requires to return a `DoraStatus` , depending of it needs to continue or stop.
  5. ```python
  6. {{#include ../../examples/python-operator-dataflow/object_detection.py:0:25}}
  7. ```
  8. > 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)
  9. ### Try it out!
  10. - Create an operator python file called `object_detection.py`:
  11. ```python
  12. {{#include ../../examples/python-dataflow/object_detection.py}}
  13. ```
  14. - Link it in your graph as:
  15. ```yaml
  16. {{#include ../../examples/python-dataflow/dataflow.yml:14:20}}
  17. ```
  18. ## Custom Node
  19. 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.
  20. #### `Node()`
  21. `Node()` initiate a node from environment variables set by `dora-coordinator`
  22. ```python
  23. from dora import Node
  24. node = Node()
  25. ```
  26. #### `.next()` or `__next__()` as an iterator
  27. `.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.
  28. ```python
  29. input_id, value, metadata = node.next()
  30. # or
  31. for input_id, value, metadata in node:
  32. ```
  33. #### `.send_output(output_id, data)`
  34. `send_output` send data from the node.
  35. ```python
  36. node.send_output("string", b"string", {"open_telemetry_context": "7632e76"})
  37. ```
  38. ### Try it out!
  39. - Install python node API:
  40. ```bash
  41. pip install dora-rs
  42. ```
  43. - Create a python file called `webcam.py`:
  44. ```python
  45. {{#include ../../examples/python-dataflow/webcam.py}}
  46. ```
  47. - Link it in your graph as:
  48. ```yaml
  49. {{#include ../../examples/python-dataflow/dataflow.yml:6:12}}
  50. ```

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