我使用Thespian开发了一个通过套接字使用低延迟数据的应用程序。我最初创建了两个角色:一个使用者(socket客户机连接)和处理程序。在测试中,我检测到在消费者(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()
结果:
^{pr2}$看看这些结果,我的代码中有什么错误或遗漏?,或者有另一种更快的方式在参与者之间发送消息?另外,还有任何其他基准可以帮助我们分析性能commented in the documentation。在
您正在将“handler”对象作为消息发送。不确定Thespian中的Actor对象有多大;Python中的空类可以从几百字节到千字节不等。我可以想象这会很快填满大量的缓冲空间。:)
相关问题 更多 >
编程相关推荐