Create Flow

An empty Flow can be created via:

from jina import Flow

f = Flow()

Use a Flow

To use f, always open it as a context manager, just like you open a file. This is considered the best practice in Jina:

with f:


Flow follows a lazy construction pattern: it won’t actually run until you use with to open it.


Once a Flow is open via with, you can send data requests to it. However, you cannot change its construction via .add() any more until it leaves the with context.


The context exits when its inner code is finished. A Flow’s context without inner code will immediately exit. To prevent that, use .block() to suspend the current process.

with f:
    f.block()  # block the current process

To terminate a blocked Flow, one can send use threading.Event or multiprocessing.Event to send the terminate signal. The following example terminates a Flow from another process.

import multiprocessing
import time

from jina import Flow

ev = multiprocessing.Event()

def close_after_5s():

f = Flow().add()
with f:
    t = multiprocessing.Process(target=close_after_5s)

Visualize a Flow

from jina import Flow

f = Flow().add().plot('f.svg')

In Jupyter Lab/Notebook, the Flow object is rendered automatically without needing to call plot().