jina.peapods.runtimes.base¶
-
class
jina.peapods.runtimes.base.
BaseRuntime
(args)[source]¶ Bases:
object
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 aBaseRuntime
class and then be used inBasePea
.In the sequel, we call the main process/thread as
M
, the process/thread blockedRuntime
asS
.In Jina, a
BasePea
object is used to manage aRuntime
object’s lifecycle. ABasePea
is a subclass ofmultiprocessing.Process
orthreading.Thread
, it starts fromM
and once theS
is spawned, it callsRuntime
methods in the following order:__init__()
inM
setup()
inS
2.
run_forever()
inS
. Note that this will blockS
, step 3 won’t be reached until it is unblocked bycancel()
3.
teardown()
inS
. Note thatS
is blocked byrun_forever()
, this step won’t be reached until step 2 is unblocked bycancel()
The
setup()
andteardown()
pair together, which defines instructions that will be executed before and after. In subclasses, they are optional.The
run_forever()
andcancel()
pair together, which introduces blocking toS
and then unblocking from it. They are mandatory for all subclasses.Note that, there is no “exclusive” relation between
run_forever()
andteardown()
,teardown()
is not about “cancelling”, it is about “cleaning”.Unlike other three methods that get invoked inside
S
, thecancel()
is invoked inM
to unblockS
. Therefore,cancel()
usually requires some special communication betweenM
andS
, e.g.Use
threading.Event
or multiprocessing.Event, whilerun_forever()
polls for this eventUse ZMQ to send a message, while
run_forever()
polls for this messageUse HTTP/REST to send a request, while
run_forever()
listens to this request
Note, another way to jump out from
run_forever()
is raise exceptions from it. This will immediately move toteardown()
.Note
Rule of thumb on exception handling: if you are not sure if you should handle exception inside
run_forever()
,cancel()
,setup()
,teardown()
, then DO NOT catch exception in them. Exception is MUCH better handled byBasePea
.See also
BasePea
for managing aRuntime
object’s lifecycle.-
run_forever
()[source]¶ Running the blocking procedure inside
S
. Note, once this method is called,S
is blocked.Note
If this method raises any exception,
teardown()
will be called.See also
cancel()
for cancelling the forever loop.
-
cancel
()[source]¶ Cancelling
run_forever()
fromM
.cancel()
usually requires some special communication betweenM
andS
, e.g.Use
threading.Event
or multiprocessing.Event, whilerun_forever()
polls for this eventUse ZMQ to send a message, while
run_forever()
polls for this messageUse HTTP/REST to send a request, while
run_forever()
listens to this request
See also
run_forever()
for blocking the process/thread.
-
setup
()[source]¶ Method called to prepare the runtime inside
S
. Optional in subclasses. The default implementation does nothing.Note
If this method raises any exception, then
run_forever()
andteardown()
won’t be called.Note
Unlike
__init__()
called inM
,setup()
is called insideS
.
-
teardown
()[source]¶ Method called immediately after
run_forever()
is unblocked. You can tidy up things here. Optional in subclasses. The default implementation does nothing.Note
This method will only be called if the
setup()
succeeds.