I have a situation where I have a processing function that can take a lot of time to run. This function takes a progress observer as an argument and notifies the observer about advancement.
I would like to send async http requests to report said progress, and return back to the processing function ASAP.
def longProcessingFunction(progressObserver):
# processing in a loop ...
progressObserver.updateProgress(x)
# more processing ...
class MyClass():
def runLongProcessingFunction(slef):
results = longProcessingFunction(self)
# send last request to report results (ideally *after* last progress message) like:
# requests.patch(self.reportingUrl, json={'results': results, 'progress': 1.0})
def updateProgress(self, progress):
# send async http request to report progress like:
# requests.patch(self.reportingUrl, json={'progress': progress})
# ideally handle the response on next progress update
# (if it arrived by that time - otherwise on the one after it)
I tried using asyncio
, but it seems that its main intention is to use a main event loop, and add multiple async tasks to it. I even tried applying a somewhat bizarre run_once approach inside the updateProgress
function, but apart from it really forcing my scenario on asyncio
I couldn't get the app to wait for the last pending progress task at the end and send the results request after it (any wait for all tasks attempt fails since I'm never really in an asyncio
loop - I'm using it to 'run once').
In my case, compared to how normal asyncio
code works, whenever I'm running the longProcessingFunction()
I'm blocked until it finishes processing (apart from the progress observer being called).
Looking for any reasonable architecture whether with asyncio
or w/o it.