I have to create a function that does some hard work in the internal calls. This function needs to be a generator because I'm using Server-Sent Events. So that, I want that this function notifies the progress of the calculations by using a "yield". After that, this function has to pass the result to the parent function in order to continue with other calculations.
I would like something like this:
def hardWork():
    for i in range(N):
        # hard work
        yield 'Work done: ' + str(i)
    # Here is the problem: I can't return a result if I use a yield
    return result             
def generator():
    # do some calculations
    result = hardWork()
    # do other calculations with this result
    yield finalResult
I found a solution that consists on yields a dictionary that tells if the function has finished or not, but the code to do this is pretty dirty.
Is there another solution?
Thank you!
EDIT
I thought something like:
def innerFunction(gen):
    calc = 1
    for iteration in range(10):
        for i in range(50000):
            calc *= random.randint(0, 10)
        gen.send(iteration)
    yield calc
def calcFunction(gen):
    gen2 = innerFunction(gen)
    r = next(gen2)
    gen.send("END: " + str(r + 1))
    gen.send(None)
def notifier():
    while True:
        x = yield
        if x is None:
            return
        yield "Iteration " + x
def generator():
    noti = notifier()
    calcFunction(noti)
    yield from noti
for g in generator():
    print(g)
But I'm receiving this error:
TypeError: can't send non-None value to a just-started generator
 
     
     
     
    