4 ms·
this is awesome. the annotation makes this way more clear cut than reading javascript. however, I'm still confused is @semisync(callback=output) mean it's
by notastartup 13y ago
this is awesome. the annotation makes this way more clear cut than reading javascript.
however, I'm still confused is
@semisync(callback=output)
mean it's asynchronous and that the output will be called after it finishes?
say I implement this in Flask, what would be the outcome of this code?
# a demo callback function
def output(field, value):
print field + ": $" + str(value)
# simple callback syntax
@app.route('/revenues')
@semisync(callback=output)
def revenue():
# simulated api call
sleep(random())
shared.revenue = randint(1, 1000)
return "Revenue", shared.revenue
If I navigate to 127.0.0.1/revenue, what would be the result? Would it output "Revenue" immediately and than a couple hundred milliseconds later print the dollar amount (from output method)? Say revenue() takes 5 seconds in total to finish (grab data, crunch numbers, finish), would it immediately show an output followed by output being called after the job processes?
This is great stuff.
- madisonmay 13y ago@semisync(callback=output) does indicate that the call is asynchronous and will be called immediately. If a dependency was specified @semisync(dependencies=[function_name]) the function will only be called once 'function_name' has been executed. In this sense, it's not entirely synchronous, and not entirely asynchronous. With regards to the flask question, you'd see an output after 5 seconds when the function finishes and returns. "Revenue" would only be printed after the function terminates. If you'd like, you could log something at the beginning of the function to let you know when execution begins.
- notastartup 13y agoIs it possible to somehow show the revenue page immediately upon requesting it (/revenue) and after 5 seconds, receive the output callback results and stick it into the DOM?
- madisonmay 13y agoI don't think that's it's possible to push to the client from the server without the client initiating a request, so I think that use case is better suited for a client side AJAX call.
- madisonmay 13y agoAlso, to clarify, the callback function must take the same position arguments that the function returns. field --> "Revenue", value --> shared.revenue in the example.