简体   繁体   中英

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.

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

use call_async instead async or for better result use event

from nameko.events import EventDispatcher, event_handler

@event_handler("service_a", "event_emit_name")
    def get_result(self, payload):
      #do_something...

and in other service

from nameko.events import EventDispatcher, event_handler
@event_handler("service_a", "event_emit_name")
    def return_result(self, payload):
       #get payload and work over there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM