BaseRuntime(args, ready_event, **kwargs)¶
A Jina Runtime is a procedure that blocks the main process once running (i.e.
run_forever()), therefore must be put into a separated thread/process. Any program/library/package/module that blocks the main process, can be formulated into a
BaseRuntimeclass and then be used in
In the sequel, we call the main process/thread as
M, the process/thread blocked
In Jina, a
BasePeaobject is used to manage a
Runtimeobject’s lifecycle. A
BasePeaacts as a
threading.Thread, it starts from
Mand once the
Sis spawned, it calls
Runtimemethods in the following order:
run_forever(). Note that this will block
S, step 3 won’t be reached until it is unblocked by
cancel(). This method is responsible to set the ready_event to guarantee that the rest of the system knows when it is ready to receive messages.
teardown()pair together, which defines instructions that will be executed before and after. In subclasses, teardown is optional.
cancel()pair together, which introduces blocking to
Sand then unblocking from it. They are mandatory for all subclasses.
Unlike other three methods that get invoked inside
cancel()is invoked in
cancel()usually requires some special communication between
Rule of thumb on exception handling: if you are not sure if you should handle exception inside
teardown(), then DO NOT catch exception in them. Exception is MUCH better handled by
BasePeafor managing a
Running the blocking procedure inside
S. Note, once this method is called,
If this method raises any exception,
teardown()will be called.
cancel()for cancelling the forever loop.