Skip to content

Commit

Permalink
docs: update sync wrapper example of examples/ folder
Browse files Browse the repository at this point in the history
  • Loading branch information
berrytern committed Oct 2, 2024
1 parent d8fb933 commit d251e3c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ config = Config(Options("queue", "rpc_queue", "rpc_exchange"))
eventbus = AsyncEventbusRabbitMQ(config)
# publish

eventbus.publish("rpc_exchange", "routing.key", "direct")
eventbus.publish("rpc_exchange", "routing.key", "message_content")
# subscribe
async def subscribe_handler(body) -> None:
print(body, type(body), flush=True) # handle messages
Expand Down Expand Up @@ -130,7 +130,7 @@ while running:
count += 1
if str(count) != eventbus.rpc_client(rpc_exchange, rpc_routing_key+"2", [f"{count}"]).decode("utf-8"):
running = False
#eventbus.publish(publish_event, rpc_routing_key, "direct")
#eventbus.publish(publish_event, rpc_routing_key, "message_content")
#running = False
except TimeoutError as err:
print("timeout!!!: ", str(err))
Expand Down Expand Up @@ -168,7 +168,7 @@ while running:
# rpc_client call
eventbus.rpc_client("rpc_exchange", "user.find", count).result().decode("utf-8")
# publish
eventbus.publish("rpc_exchange", "routing.key", "direct").result()
eventbus.publish("rpc_exchange", "routing.key", "message_content").result()
#running = False
except KeyboardInterrupt:
running=False
Expand Down
4 changes: 2 additions & 2 deletions examples/async_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ async def run():
try:
count += 1
result = await eventbus.rpc_client(
rpc_exchange, "user.find", ["content_message"]
rpc_exchange, "user.find", ["message_content"]
)
print("returned:", result)
await eventbus.publish(rpc_exchange, "user.find3", ["content_message"])
await eventbus.publish(rpc_exchange, "user.find3", ["message_content"])
except BaseException as err:
print(f"err: {err}")

Expand Down
27 changes: 7 additions & 20 deletions examples/sync_wrapper_case.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from amqp_client_python import EventbusWrapperRabbitMQ, Config, Options, SSLOptions
from amqp_client_python.event import IntegrationEvent, AsyncSubscriberHandler
from amqp_client_python import EventbusWrapperRabbitMQ, Config, Options # , SSLOptions
from default import queue, rpc_queue, rpc_exchange, rpc_routing_key


Expand All @@ -10,17 +9,8 @@
eventbus = EventbusWrapperRabbitMQ(config=config)


class ExampleEvent(IntegrationEvent):
EVENT_NAME: str = "ExampleEvent"

def __init__(self, event_type: str, message=[]) -> None:
super().__init__(self.EVENT_NAME, event_type)
self.message = message


class ExampleEventHandler(AsyncSubscriberHandler):
async def handle(self, body) -> None:
print(body, "subscribe")
async def subscribe_handler(body) -> None:
print(body, "subscribe")


async def handle(*body):
Expand All @@ -37,10 +27,7 @@ async def handle2(*body):
return f"{body[0]}".encode("utf-8")


subscribe_event = ExampleEvent(rpc_exchange)
publish_event = ExampleEvent(rpc_exchange, ["message"])
subscribe_event_handle = ExampleEventHandler()
eventbus.subscribe(subscribe_event, subscribe_event_handle, rpc_routing_key).result()
eventbus.subscribe(rpc_exchange, rpc_routing_key, subscribe_handler).result()
eventbus.provide_resource(rpc_routing_key + "2", handle).result()
eventbus.provide_resource(rpc_routing_key + "3", handle2).result()
count = 0
Expand All @@ -49,15 +36,15 @@ async def handle2(*body):
try:
count += 1
if str(count) != eventbus.rpc_client(
rpc_exchange, rpc_routing_key + "2", [f"{count}"]
rpc_exchange, rpc_routing_key + "2", count
).result().decode("utf-8"):
running = False
eventbus.publish(publish_event, rpc_routing_key, "direct").result()
eventbus.publish(rpc_exchange, rpc_routing_key, "message_content").result()
# running = False
except KeyboardInterrupt:
running = False
except BaseException as err:
running = False
print("Err:", err)

eventbus.dispose()
eventbus.dispose()

0 comments on commit d251e3c

Please sign in to comment.