I want a function that sends other functions (without executing them, but with the input stored inside) to start execution on other node of my computing clúster. The first function calculates the inputs of the other function.
The problem is that yield executes the function at the begining of the iteration, but I want only the function to execute it with a different node of my cluster.
I have created the following code to explain myself:
def calculate(a: int,b: int ,operation: str):
print("Working...")
if operation == 'multiply':
return a * b
elif operation == 'sum':
return a+b
def calculation_sender(a: [int], b:[int], operations: [str]):
for i in range(len(operations)):
yield calculate(a[i],b[i], operations[i])
a = [1,2,3]
b = [1,2,3]
operations = ['sum','multiply','sum']
for calculation in calculation_sender(a, b, operations):
print("Don't work yet!")
print(calculation)
Output:
Working...
Don't work yet!
2
Working...
Don't work yet!
4
Working...
Don't work yet!
6
It is possible to return a function with parameters but without calling it?
functools.partial
import functools
def calculate(a: int,b: int ,operation: str):
print("Working...")
if operation == 'multiply':
return functools.partial(mul,a,b)
elif operation == 'sum':
return functools.partial(add,a,b)
def add(a, b):
return a + b
def mul(a, b):
return a * b
#return a function with parameters but without calling it
print(calculate(3,5,"sum"))
#when you need to call it, just add a "()"
print(calculate(3,5,"sum")())
Working...
functools.partial(<function add at 0x000002029FD94168>, 3, 5)
Working...
8