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.html 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!DOCTYPE HTML>
  2. <html lang="en" class="sidebar-visible no-js light">
  3. <head>
  4. <!-- Book generated using mdBook -->
  5. <meta charset="UTF-8">
  6. <title>Python API - dora-rs</title>
  7. <!-- Custom HTML head -->
  8. <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  9. <meta name="description" content="">
  10. <meta name="viewport" content="width=device-width, initial-scale=1">
  11. <meta name="theme-color" content="#ffffff" />
  12. <link rel="icon" href="favicon.svg">
  13. <link rel="shortcut icon" href="favicon.png">
  14. <link rel="stylesheet" href="css/variables.css">
  15. <link rel="stylesheet" href="css/general.css">
  16. <link rel="stylesheet" href="css/chrome.css">
  17. <link rel="stylesheet" href="css/print.css" media="print">
  18. <!-- Fonts -->
  19. <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
  20. <link rel="stylesheet" href="fonts/fonts.css">
  21. <!-- Highlight.js Stylesheets -->
  22. <link rel="stylesheet" href="highlight.css">
  23. <link rel="stylesheet" href="tomorrow-night.css">
  24. <link rel="stylesheet" href="ayu-highlight.css">
  25. <!-- Custom theme stylesheets -->
  26. </head>
  27. <body>
  28. <!-- Provide site root to javascript -->
  29. <script type="text/javascript">
  30. var path_to_root = "";
  31. var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
  32. </script>
  33. <!-- Work around some values being stored in localStorage wrapped in quotes -->
  34. <script type="text/javascript">
  35. try {
  36. var theme = localStorage.getItem('mdbook-theme');
  37. var sidebar = localStorage.getItem('mdbook-sidebar');
  38. if (theme.startsWith('"') && theme.endsWith('"')) {
  39. localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
  40. }
  41. if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
  42. localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
  43. }
  44. } catch (e) { }
  45. </script>
  46. <!-- Set the theme before any content is loaded, prevents flash -->
  47. <script type="text/javascript">
  48. var theme;
  49. try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
  50. if (theme === null || theme === undefined) { theme = default_theme; }
  51. var html = document.querySelector('html');
  52. html.classList.remove('no-js')
  53. html.classList.remove('light')
  54. html.classList.add(theme);
  55. html.classList.add('js');
  56. </script>
  57. <!-- Hide / unhide sidebar before it is displayed -->
  58. <script type="text/javascript">
  59. var html = document.querySelector('html');
  60. var sidebar = 'hidden';
  61. if (document.body.clientWidth >= 1080) {
  62. try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
  63. sidebar = sidebar || 'visible';
  64. }
  65. html.classList.remove('sidebar-visible');
  66. html.classList.add("sidebar-" + sidebar);
  67. </script>
  68. <nav id="sidebar" class="sidebar" aria-label="Table of contents">
  69. <div class="sidebar-scrollbox">
  70. <ol class="chapter"><li class="chapter-item expanded affix "><a href="introduction.html">Introduction</a></li><li class="spacer"></li><li class="chapter-item expanded affix "><li class="part-title">User Guide</li><li class="chapter-item expanded "><a href="installation.html"><strong aria-hidden="true">1.</strong> installation</a></li><li class="chapter-item expanded "><a href="getting-started.html"><strong aria-hidden="true">2.</strong> Getting started</a></li><li class="chapter-item expanded affix "><li class="part-title">Reference Guide</li><li class="chapter-item expanded "><a href="overview.html"><strong aria-hidden="true">3.</strong> Overview</a></li><li class="chapter-item expanded "><a href="dataflow-config.html"><strong aria-hidden="true">4.</strong> Dataflow Configuration</a></li><li class="chapter-item expanded "><a href="rust-api.html"><strong aria-hidden="true">5.</strong> Rust API</a></li><li class="chapter-item expanded "><a href="c-api.html"><strong aria-hidden="true">6.</strong> C API</a></li><li class="chapter-item expanded "><a href="python-api.html" class="active"><strong aria-hidden="true">7.</strong> Python API</a></li><li class="chapter-item expanded affix "><li class="part-title">Brainstorming Ideas</li><li class="chapter-item expanded "><a href="state-management.html"><strong aria-hidden="true">8.</strong> State Management</a></li><li class="chapter-item expanded "><a href="library-vs-framework.html"><strong aria-hidden="true">9.</strong> Library vs Framework</a></li><li class="chapter-item expanded "><a href="communication-layer.html"><strong aria-hidden="true">10.</strong> Middleware Layer Abstraction</a></li></ol> </div>
  71. <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
  72. </nav>
  73. <div id="page-wrapper" class="page-wrapper">
  74. <div class="page">
  75. <div id="menu-bar-hover-placeholder"></div>
  76. <div id="menu-bar" class="menu-bar sticky bordered">
  77. <div class="left-buttons">
  78. <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
  79. <i class="fa fa-bars"></i>
  80. </button>
  81. <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
  82. <i class="fa fa-paint-brush"></i>
  83. </button>
  84. <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
  85. <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li>
  86. <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
  87. <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
  88. <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
  89. <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
  90. </ul>
  91. <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
  92. <i class="fa fa-search"></i>
  93. </button>
  94. </div>
  95. <h1 class="menu-title">dora-rs</h1>
  96. <div class="right-buttons">
  97. <a href="print.html" title="Print this book" aria-label="Print this book">
  98. <i id="print-button" class="fa fa-print"></i>
  99. </a>
  100. </div>
  101. </div>
  102. <div id="search-wrapper" class="hidden">
  103. <form id="searchbar-outer" class="searchbar-outer">
  104. <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
  105. </form>
  106. <div id="searchresults-outer" class="searchresults-outer hidden">
  107. <div id="searchresults-header" class="searchresults-header"></div>
  108. <ul id="searchresults">
  109. </ul>
  110. </div>
  111. </div>
  112. <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
  113. <script type="text/javascript">
  114. document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
  115. document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
  116. Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
  117. link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
  118. });
  119. </script>
  120. <div id="content" class="content">
  121. <main>
  122. <h1 id="python-api"><a class="header" href="#python-api">Python API</a></h1>
  123. <h2 id="operator"><a class="header" href="#operator">Operator</a></h2>
  124. <p>The operator API is a framework for you to implement. The implemented operator will be managed by <code>dora</code>. This framework enable us to make optimisation and provide advanced features. It is the recommended way of using <code>dora</code>.</p>
  125. <p>An operator requires an <code>on_input</code> method and requires to return a <code>DoraStatus</code> of 0 or 1, depending of it needs to continue or stop.</p>
  126. <pre><code class="language-python">class Operator:
  127. def on_input(
  128. self,
  129. input_id: str,
  130. value: bytes,
  131. send_output: Callable[[str, bytes], None],
  132. ) -&gt; DoraStatus:
  133. </code></pre>
  134. <blockquote>
  135. <p>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: <a href="https://docs.rs/pyo3/latest/pyo3/marker/struct.Python.html#deadlocks">https://docs.rs/pyo3/latest/pyo3/marker/struct.Python.html#deadlocks</a></p>
  136. </blockquote>
  137. <h3 id="try-it-out"><a class="header" href="#try-it-out">Try it out!</a></h3>
  138. <ul>
  139. <li>Create an operator python file called <code>op.py</code>:</li>
  140. </ul>
  141. <pre><code class="language-python">from typing import Callable
  142. from enum import Enum
  143. class DoraStatus(Enum):
  144. CONTINUE = 0
  145. STOP = 1
  146. class Operator:
  147. &quot;&quot;&quot;
  148. Example operator incrementing a counter every times its been called.
  149. The current value of the counter is sent back to dora on `counter`.
  150. &quot;&quot;&quot;
  151. def __init__(self, counter=0):
  152. self.counter = counter
  153. def on_input(
  154. self,
  155. input_id: str,
  156. value: bytes,
  157. send_output: Callable[[str, bytes], None],
  158. ):
  159. &quot;&quot;&quot;Handle input by incrementing count by one.
  160. Args:
  161. input_id (str): Id of the input declared in the yaml configuration
  162. value (bytes): Bytes message of the input
  163. send_output (Callable[[str, bytes]]): Function enabling sending output back to dora.
  164. &quot;&quot;&quot;
  165. val_len = len(value)
  166. print(f&quot;PYTHON received input {input_id}; value length: {val_len}&quot;)
  167. send_output(&quot;counter&quot;, (self.counter % 256).to_bytes(1, &quot;little&quot;))
  168. self.counter = self.counter + 1
  169. return DoraStatus.OK
  170. </code></pre>
  171. <ul>
  172. <li>Link it in your graph as:</li>
  173. </ul>
  174. <pre><code class="language-yaml">{{#include ../../binaries/coordinator/examples/graphs/mini-dataflow.yml:67:73}}
  175. </code></pre>
  176. <h2 id="custom-node"><a class="header" href="#custom-node">Custom Node</a></h2>
  177. <p>The custom node API allow you to integrate <code>dora</code> into your application. It allows you to retrieve input and send output in any fashion you want.</p>
  178. <h4 id="node"><a class="header" href="#node"><code>Node()</code></a></h4>
  179. <p><code>Node()</code> initiate a node from environment variables set by <code>dora-coordinator</code> </p>
  180. <pre><code class="language-python">from dora import Node()
  181. node = Node()
  182. </code></pre>
  183. <h4 id="next-or-__next__-as-an-iterator"><a class="header" href="#next-or-__next__-as-an-iterator"><code>.next()</code> or <code>__next__()</code> as an iterator</a></h4>
  184. <p><code>.next()</code> gives you the next input that the node has received. It blocks until the next input becomes available. It will return <code>None</code> when all senders has been dropped.</p>
  185. <pre><code class="language-python">input_id, value = node.next()
  186. # or
  187. for input_id, value in node:
  188. </code></pre>
  189. <h4 id="send_outputoutput_id-data"><a class="header" href="#send_outputoutput_id-data"><code>.send_output(output_id, data)</code></a></h4>
  190. <p><code>send_output</code> send data from the node.</p>
  191. <pre><code class="language-python">node.send_output(&quot;string&quot;, b&quot;string&quot;)
  192. </code></pre>
  193. <h3 id="try-it-out-1"><a class="header" href="#try-it-out-1">Try it out!</a></h3>
  194. <ul>
  195. <li>Install python node API:</li>
  196. </ul>
  197. <pre><code class="language-bash">cd apis/python/node
  198. python3 -m venv .env
  199. source .env/bin/activate
  200. pip install maturin
  201. maturin develop
  202. </code></pre>
  203. <ul>
  204. <li>Create a python file called <code>printer.py</code>:</li>
  205. </ul>
  206. <pre><code class="language-python">from dora import Node
  207. node = Node()
  208. for id, value in node:
  209. print(f&quot;From Python, id: {id}, value: {value}&quot;) if value is not [] else None
  210. print(&quot;printer finished&quot;)
  211. </code></pre>
  212. <ul>
  213. <li>Link it in your graph as:</li>
  214. </ul>
  215. <pre><code class="language-yaml"> - id: python-printer
  216. custom:
  217. run: python examples/nodes/python/printer.py
  218. inputs:
  219. string: static-string/string
  220. time2: rust-timer/time
  221. </code></pre>
  222. </main>
  223. <nav class="nav-wrapper" aria-label="Page navigation">
  224. <!-- Mobile navigation buttons -->
  225. <a rel="prev" href="c-api.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
  226. <i class="fa fa-angle-left"></i>
  227. </a>
  228. <a rel="next" href="state-management.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
  229. <i class="fa fa-angle-right"></i>
  230. </a>
  231. <div style="clear: both"></div>
  232. </nav>
  233. </div>
  234. </div>
  235. <nav class="nav-wide-wrapper" aria-label="Page navigation">
  236. <a rel="prev" href="c-api.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
  237. <i class="fa fa-angle-left"></i>
  238. </a>
  239. <a rel="next" href="state-management.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
  240. <i class="fa fa-angle-right"></i>
  241. </a>
  242. </nav>
  243. </div>
  244. <script type="text/javascript">
  245. window.playground_copyable = true;
  246. </script>
  247. <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
  248. <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
  249. <script src="searcher.js" type="text/javascript" charset="utf-8"></script>
  250. <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
  251. <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
  252. <script src="book.js" type="text/javascript" charset="utf-8"></script>
  253. <!-- Custom JS scripts -->
  254. </body>
  255. </html>

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