Flow

Flow ties Executors together into a processing pipeline to perform a bigger task, like indexing or querying a dataset. Documents “flow” through the created pipeline and are processed by Executors. Flow also provides synchronization mechanisms to manage dependencies between executors and their order.

A Flow object has the following common methods:

GroupDescription
Construct Flow.add(), .needs()
Run Flowwith context manager
Visualize Flow.plot()
Send Request.post()
Control.block()

Minimum working example

All-in-one style

  1. from jina import Flow, Document, Executor, requests
  2. class MyExecutor(Executor):
  3. @requests(on='/bar')
  4. def foo(self, docs, **kwargs):
  5. print(docs)
  6. f = Flow().add(name='myexec1', uses=MyExecutor)
  7. with f:
  8. f.post(on='/bar', inputs=Document(), on_done=print)

Flow-as-a-Service style

Server:

  1. from jina import Flow, Executor, requests
  2. class MyExecutor(Executor):
  3. @requests(on='/bar')
  4. def foo(self, docs, **kwargs):
  5. print(docs)
  6. f = Flow(port_expose=12345).add(name='myexec1', uses=MyExecutor)
  7. with f:
  8. f.block()

Client:

  1. from jina import Client, Document
  2. c = Client(port=12345)
  3. c.post(on='/bar', inputs=Document(), on_done=print)

Load from YAML

my.yml:

  1. jtype: Flow
  2. executors:
  3. - name: myexec1
  4. uses: MyExecutor
  1. from jina import Flow, Document
  2. f = Flow.load_config('my.yml')
  3. with f:
  4. f.post(on='/bar', inputs=Document(), on_done=print)

See Also

Document, Executor, and Flow are the three fundamental concepts in Jina.

  • Document is the basic data type in Jina;

  • Executor is how Jina processes Documents;

  • Flow is how Jina streamlines and scales Executors.