Open goodboy opened 7 years ago
So re: this that you said elsewhere:
def is_alive(self): def alive(): listener.is_alive() return any(self.pool.evals(alive))
has to lookup listener from somewhere. eval makes this simple by just handing it a namespace but invoking functions means probably building functools.partials or something similar.
That example works just fine if you only change one thing: instead of assuming the evals
argument is a string, check if it has __code__
defined on it, and if so send that to eval
instead. The namespace injection thing in eval works just fine with both string and code objects being evalled.
The idea I think would be better goes a bit further: instead of injecting into the namespace using eval
you can require that the call to pool.evals
use a function/lambda and extract the named arguments it expects from callable.__code__.co_varnames
. That means you can just write the inline lambda like
def is_alive(self):
return any(self.pool.evals(lambda listener: listener.is_alive()))
then in the evals
call you'd have something like
if func.__code__.co_varnames[0] == "listener":
func(listener)
elif func.__code__.co_varnames[0] == "client":
func(client)
and then you don't even need the eval call. (And it'd probably make more sense if that's what you do to rename evals
to run_on_slaves
or something.
I'm seriously on board with this after having used salt more extensively now. Hopefully this can get in for a next minor release :)
@dtkerr I'd of course take a PR if you feel so inclined ;)
The following is posted verbatim from @dtkerrs review of #49 with regard to the
switchy.distribute.MultiEval
interface and implementation:@vodik also commented: