低性能使用Thespian在演员之间发送低延迟消息

时间:2018-05-24 01:54:13

标签: python actor

我使用Thespian开发一个应用程序来通过套接字消耗低延迟数据。我最初创建了两个actor:一个使用者(套接字客户端连接)和处理程序。在测试中,我检测到在消费者(Actor1)中发送一条消息的时间与该消息到达处理程序(Actor2)的时间之间存在一些延迟。

为了测试它,我使用了以下代码。

from thespian.actors import ActorSystem, Actor

import time


class Actor1(Actor):
    def receiveMessage(self, handler, sender):
        # benchmark
        data = {'tic': time.perf_counter(), 'lim': 10000}
        print('Elapsed Time to process {} messages'.format(data['lim']))
        for i in range(data['lim']):
            self.send(handler, data)
        # perf
        toc = time.perf_counter() - data['tic']
        print('Actor 1: {} sec'.format(round(toc, 3)))
        # self.actorSystemShutdown()


class Actor2(Actor):
    def __init__(self):
        self.msg_count = 0

    def receiveMessage(self, data, sender):
        self.msg_count += 1
        if self.msg_count == data['lim']:
            toc = time.perf_counter() - data['tic']
            print('Actor 2: {} sec'.format(round(toc, 3)))

def main():
    asys = ActorSystem('multiprocTCPBase')
    consumer = asys.createActor(Actor1)
    handler = asys.createActor(Actor2)
    asys.tell(consumer, handler)

if __name__ == '__main__':
    main()

结果:

Elapsed Time to process 10 messages
Actor 1: 0.002 sec
Actor 2: 0.008 sec

Elapsed Time to process 100 messages
Actor 1: 0.019 sec
Actor 2: 0.099 sec

Elapsed Time to process 1000 messages
Actor 1: 0.131 sec
Actor 2: 0.769 sec

Elapsed Time to process 10000 messages
Actor 1: 1.219 sec
Actor 2: 7.608 sec

Elapsed Time to process 100000 messages
Actor 1: 22.012 sec
Actor 2: 91.419 sec

查看这些结果,我的代码中有错误或遗漏了什么?或者还有另一种更快的方式在演员之间发送消息?此外,还有其他任何基准可帮助我们分析效果commented in the documentation

1 个答案:

答案 0 :(得分:0)

您正在将“ handler”对象作为消息发送。不确定Thespian中Actor对象的大小; Python中的空类的范围可以从几百个字节到一千个字节。我可以想象这最终很快就会填满大量的缓冲区空间。 :)