0

I'm calling several functions from one main function mainFunc (which is exposed as Restful service using flask module). Execution may take few minutes to an hour to complete and requirement is to display progress (output of functions being called) every 30 seconds. As Rest API calls are made from another program to mainFunc() so it should return the output (i.e current status) at each call.

I tried yield and StringIO as per How to capture stdout output from a Python function call? but could not make it work, stdout is returned only on completion of all tasks and not in between. Please suggest if this can be done.

mainFunc():
    runs_for_5_mins()
    runs_for_30_mins()  
    ...
    return output
Community
  • 1
  • 1
redeye
  • 3
  • 2

1 Answers1

0

Of course it can be done. The question is only how complicated it should be ;-)

The best approach for progress output is to tweak the long running routine so that it does this output itself. The routine is the place where the information is present how much has been done and how much is still ahead, so nobody else can be as precise as the routine itself. A routine can be given a special stream (e. g. an open file handle) to put the progress information into. You as the caller then could simply pass sys.stderr or similar.

But if you are not willing or able to change your routines then let's face it: You want to do two things in parallel:

  1. The calls to the routines from your mainFunc() function and
  2. The progress outputs.

For doing things in parallel within one process you use threads. So

  1. start off your progress thread which each 30 seconds prints some progress output,
  2. start your routines,
  3. after finishing the routines signal the thread to terminate itself.

If this is the way you want to solve your problem I could go into detail here.

Alfe
  • 47,603
  • 17
  • 84
  • 139