-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(examples): Better minimal example
Branch: main Signed-off-by: Gabe Goodhart <[email protected]>
- Loading branch information
1 parent
3c5303e
commit b7179f1
Showing
3 changed files
with
77 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,11 @@ | ||
""" | ||
Minimal example of multiple listeners on the message queue | ||
""" | ||
|
||
# Standard | ||
from functools import partial | ||
import operator | ||
|
||
# First Party | ||
from caikit.core import DataObjectBase, dataobject | ||
|
||
# Local | ||
from caikit.interfaces.common.data_model import StrSequence | ||
from caikit_compose import MQ_FACTORY, Message | ||
|
||
|
||
@dataobject | ||
class Number(DataObjectBase): | ||
val: float | ||
|
||
|
||
def operation(mq, topic, c, oper, msg): | ||
if op := getattr(operator, oper, None): | ||
mq.publish( | ||
topic, | ||
Message.from_data( | ||
Number(op(msg.unwrapped.val, c)), | ||
data_id=msg.header.data_id, | ||
metadata={"oper": oper}, | ||
), | ||
) | ||
|
||
|
||
def report(msg): | ||
oper = msg.nested_get("metadata.oper") | ||
print(f"RESULT {oper}: {msg.unwrapped.val}") | ||
|
||
|
||
if __name__ == "__main__": | ||
mq = MQ_FACTORY.construct({"type": "LOCAL", "config": {"threads": 0}}) | ||
mq.create_topic("input") | ||
mq.create_topic("output") | ||
c_val = float(input("C Val: ")) | ||
for op in ["mul", "truediv", "add", "sub"]: | ||
mq.subscribe("input", op, partial(operation, mq, "output", c_val, op)) | ||
mq.subscribe("output", "", report) | ||
while True: | ||
x = float(input("X: ")) | ||
mq.publish("input", Message.from_data(Number(x))) | ||
mq = MQ_FACTORY.construct({"type":"LOCAL"}) | ||
mq.create_topic("input") | ||
def greet(msg: Message): | ||
for name in msg.unwrapped.values: | ||
print(f"Hello {name}!") | ||
mq.subscribe("input", "", greet) | ||
while True: | ||
x = input("X: ") | ||
mq.publish("input", Message.from_data(StrSequence(x.split(",")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
""" | ||
Minimal example of multiple listeners on the message queue | ||
""" | ||
|
||
# Standard | ||
from functools import partial | ||
import operator | ||
|
||
# First Party | ||
from caikit.core import DataObjectBase, dataobject | ||
|
||
# Local | ||
from caikit_compose import MQ_FACTORY, Message | ||
|
||
|
||
@dataobject | ||
class Number(DataObjectBase): | ||
val: float | ||
|
||
|
||
def operation(mq, topic, c, oper, msg): | ||
if op := getattr(operator, oper, None): | ||
mq.publish( | ||
topic, | ||
Message.from_data( | ||
Number(op(msg.unwrapped.val, c)), | ||
data_id=msg.header.data_id, | ||
metadata={"oper": oper}, | ||
), | ||
) | ||
|
||
|
||
def report(msg): | ||
oper = msg.nested_get("metadata.oper") | ||
print(f"RESULT {oper}: {msg.unwrapped.val}") | ||
|
||
|
||
if __name__ == "__main__": | ||
mq = MQ_FACTORY.construct({"type": "LOCAL", "config": {"threads": 0}}) | ||
mq.create_topic("input") | ||
mq.create_topic("output") | ||
c_val = float(input("C Val: ")) | ||
for op in ["mul", "truediv", "add", "sub"]: | ||
mq.subscribe("input", op, partial(operation, mq, "output", c_val, op)) | ||
mq.subscribe("output", "", report) | ||
while True: | ||
x = float(input("X: ")) | ||
mq.publish("input", Message.from_data(Number(x))) |