如何在Nameko中使RPC调用异步?

时间:2019-12-30 10:07:53

标签: python rabbitmq rpc nameko

我正在使用nameko构建具有微服务体系结构的ETL管道,并且我不想在发出RPC请求后等待答复。

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)

此代码引发错误:

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

我是微服务和Nameko的新手,并且我正在使用RabbitMQ作为排队服务。

2 个答案:

答案 0 :(得分:1)

我有同样的问题;您需要用async替换call_async方法,并用result()检索数据。

Documentation
GitHub issue

答案 1 :(得分:0)

使用call_async代替async或为了获得更好的结果而使用事件

从nameko.events导入EventDispatcher,event_handler

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

和其他服务

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