nameko中的定时器~nameko系列之四

2018-11-13  本文已影响0人  andrew_su_cd

定时操作是许多应用都需要使用的一个功能。
在python的web开发世界中,芹菜 celery是一个非常好的解决方案。工作原理是运行一个daemon,定期扫描redis或者库中的记录,然后执行相应的任务。

在nameko中,做定时会更加自然,在微服务类中的每个方法,都可以随时变成一个定时执行的工作狂。

from nameko.timer import timer
from nameko.rpc import rpc,RpcProxy

class ServiceA:
    name = "serviceA"
    serviceB = RpcProxy('serviceB')
    
    #采用定时器的方式调用另外微服务的方法
    @timer(5)
    @rpc
    def periodically_call_rpc_method(self):
        print(self.serviceB.provide_service_for_others()        

    #普通定时器
    @timer(10)
    def periodically_say_hello(self):
        print('I am a common timer')        

class ServiceB:
    name = 'serviceB'

    @rpc
    def provide_service_for_others(self):
        return 'hello,I am a rpc service for you'

说明:

上一篇 下一篇

猜你喜欢

热点阅读