【发布时间】:2019-09-05 12:57:43
【问题描述】:
我正在尝试使用 Thespian (https://thespianpy.com/doc/),这是一个用于演员模型的 Python 库,特别是我正在尝试使用“剧团”功能。据我了解,troupe 装饰器充当调度程序,以运行多个 actor 直到指定的 max_count,每个 actor 并行运行。剧团功能用作我的演员类的装饰器:
@troupe(max_count = 4, idle_count = 2)
class Calculation(ActorTypeDispatcher):
def receiveMsg_CalcMsg(self, msg, sender):
self.send(sender, long_process(msg.index, msg.value, msg.status_cb))
我想在运行时而不是设计时配置 max_count。我承认我对装饰器的基础知识很薄弱。
如何在运行时将值传递给 max_count?
我已经经历了这些,但我仍然在黑暗中:
Does python allow me to pass dynamic variables to a decorator at runtime?
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
根据到目前为止的答案,我尝试了这个,但没有应用装饰器(即它表现得好像没有装饰器)。我注释掉了类上方的@troupe 实现,该方法(包括变量)工作正常。这种方法不是:
# @troupe(max_count=cores, idle_count=2)
class Calculation(ActorTypeDispatcher):
def receiveMsg_CalcMsg(self, msg, sender):
self.send(sender, long_process(msg.index, msg.value, msg.status_cb))
def calculate(asys, calc_values, status_cb):
decorated_class = troupe(max_count=5, idle_count=2)(Calculation)
calc_actor = asys.createActor(decorated_class)
calculate 函数中还有其他内容,但这几乎只是一些记账。
【问题讨论】: