Search code examples
pythonrabbitmqrpcnameko

How to make RPC calls asynchronous in nameko?


I am using nameko to build an ETL pipeline with a micro-service architecture, and I do not want to wait for a reply after making a RPC request.

from nameko.rpc import rpc, RpcProxy

class Scheduler(object):
  name = "scheduler"

  task_runner = RpcProxy('task_runner')

  @rpc
  def schedule(self, task_type, group_id, time):
     return self.task_runner.start.async(task_type, group_id)

This code throws an error:

Traceback (most recent call last):
  File "/home/satnam-sandhu/.anaconda3/envs/etl/bin/nameko", line 8, in <module>
    sys.exit(main())
  File "/home/satnam-sandhu/.anaconda3/envs/etl/lib/python3.8/site-packages/nameko/cli/main.py", line 112, in main
    args.main(args)
  File "/home/satnam-sandhu/.anaconda3/envs/etl/lib/python3.8/site-packages/nameko/cli/commands.py", line 110, in main
    main(args)
  File "/home/satnam-sandhu/.anaconda3/envs/etl/lib/python3.8/site-packages/nameko/cli/run.py", line 181, in main
    import_service(path)
  File "/home/satnam-sandhu/.anaconda3/envs/etl/lib/python3.8/site-packages/nameko/cli/run.py", line 46, in import_service
    __import__(module_name)
  File "./scheduler/service.py", line 15
    return self.task_runner.start.async(task_type, group_id)
                                  ^
SyntaxError: invalid syntax

I am new with microservices and Nameko, and also I am using RabbitMQ as the queuing service.


Solution

  • I had the same problem; you need to replace the async method with the call_async one, and retrieve the data with result().

    Documentation
    GitHub issue