@@ -9,7 +9,7 @@ For a full example, using FastAPI, Redis, and protobunny, see [this repo](https:
99## Setup
1010
1111### pyproject.toml
12- Add ` protobunny ` to your ` pyproject.toml ` dependencies:
12+ Add ` protobunny ` to your ` pyproject.toml ` dependencies (add the backend you need as extra dependency) :
1313
1414``` shell
1515uv add protobunny[rabbitmq, numpy]
@@ -325,7 +325,7 @@ messages-directory = "messages"
325325messages-prefix = " acme"
326326generated-package-name = " mymessagelib"
327327generated-package-root = " codegen"
328- backend = " rabbitmq"
328+ backend = " rabbitmq" # configure here the backend (choose between rabbitmq, redis, mosquitto, nats, python)
329329mode = " async"
330330```
331331
@@ -376,22 +376,19 @@ You should find the generated classes under `codegen/mymessagelib`.
376376### Python code to test the library
377377
378378``` python
379-
380379import asyncio
381380import logging
382- import sys
381+ import time
383382
384383import protobunny as pb
385384from protobunny import asyncio as pb_asyncio
386-
387- # sys.path.append(pb.default_configuration.generated_package_root)
388- # this is needed when the python classes for your lib are generated in a subfolder
389- # that is not in the namespace and it must not be treated as a package
390- # It's just a sys.path.append("./codegen")
391- pb.config_lib()
385+ # # sys.path.append(pb.default_configuration.generated_package_root)
386+ # sys.path.append("./codegen")
387+ pb.config_lib() # this is needed when the python classes for your lib are generated in a subfolder
392388
393389import mymessagelib as ml
394390
391+
395392logging.basicConfig(
396393 level = logging.INFO , format = " [%(asctime)s %(levelname)s ] %(name)s - %(message)s "
397394)
@@ -411,20 +408,24 @@ class TestLibAsync:
411408
412409 async def worker1 (self , task : ml.main.tasks.TaskMessage) -> None :
413410 log.info(" 1- Working on: %s " , task)
411+ await asyncio.sleep(0.1 )
414412
415413 async def worker2 (self , task : ml.main.tasks.TaskMessage) -> None :
416414 log.info(" 2- Working on: %s " , task)
415+ await asyncio.sleep(0.1 )
417416
418417 async def on_message_mymessage (self , message : ml.main.MyMessage) -> None :
419418 log.info(" Got main message: %s " , message)
420419
420+
421421 def run_forever (self ):
422422 asyncio.run(self .main())
423423
424424 def log_callback (self , incoming , body ) -> None :
425425 log.info(f " LOG { incoming.routing_key} : { body} " )
426426
427427 async def main (self ):
428+
428429 await pb_asyncio.subscribe_logger(self .log_callback)
429430 await pb_asyncio.subscribe(ml.main.tasks.TaskMessage, self .worker1)
430431 await pb_asyncio.subscribe(ml.main.tasks.TaskMessage, self .worker2)
@@ -454,9 +455,11 @@ class TestLib:
454455
455456 def worker1 (self , task : ml.main.tasks.TaskMessage) -> None :
456457 log.info(" 1- Working on: %s " , task)
458+ time.sleep(0.1 )
457459
458460 def worker2 (self , task : ml.main.tasks.TaskMessage) -> None :
459461 log.info(" 2- Working on: %s " , task)
462+ time.sleep(0.1 )
460463
461464 def log_callback (self , incoming , body ) -> None :
462465 log.info(f " LOG { incoming.routing_key} : { body} " )
@@ -478,7 +481,8 @@ class TestLib:
478481
479482
480483if __name__ == " __main__" :
481- if conf.use_async:
484+ config = pb.config
485+ if config.use_async:
482486 log.info(" Using async" )
483487 testlib = TestLibAsync()
484488 pb_asyncio.run_forever(testlib.main)
0 commit comments