0

I have a command in a line (Fit.perform() from import xspec, but never mind because the question is general and can be applicated also for other python commands) that takes a while to finish.

I simple want to know the time of execution while the command is running, so when it has not finished its execution yet. This is necessary if I want to stop the command during its execution, for example because it is taking too much time to end.

So, I need something like this:

if **you_are_taking_so_much_time**:
    do_something_else

It is not possible to use methods like time or timeit because they calculate the time only at the end of execution of a command and not while it is running.

Is it possible?

I'm using python 2.7 on MacOS.

Alessandro Peca
  • 873
  • 1
  • 15
  • 40

2 Answers2

1

You will have to use a monitor thread:

import threading
import time

done = False

def longfun():
    global done
    print("This will take some time.")
    time.sleep(60)
    done = True

def monitor():
    global done
    timeout = 10
    print("Wait until timeout.")
    while not done and timeout > 0:
        time.sleep(1)
        timeout -= 1

lt = threading.Thread(target=longfun)
lt.start()
mt = threading.Thread(target=monitor)
mt.start()

mt.join()
if done == False:
    print("Long thread not done yet. Do something else.")

lt.join()

Note that this does wait until the 'long' thread is finished. You do not mention you want to stop the long running operation. If you do, you will have to correctly implement it in a thread, including start/stop/progress functionality (usually this works with a while loop that uses a running bit to see if it should continue.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
  • Thank you for the answer, but where I have to put my command `Fit.perform()` ? I have only one line (`Fit.perform()`) to monitor. – Alessandro Peca Jan 11 '18 at 09:26
  • @AlessandroPeca, you place that instead of the `time.sleep(60)` call. I put that in as a place holder for a long running, blocking operation. – Bart Friederichs Jan 11 '18 at 10:00
  • I'm sorry but I tried in various ways and does not seem to work, you can give me an example with the function to monitor inserted in the middle of a code? – Alessandro Peca Jan 12 '18 at 13:26
0

like this:

import time,thread
def test_me(hargs):
    func,args,timeout = hargs
    start_time = time.time()
    thread.start_newthread(func,args)
    while True :
        if My_expected_value:#where store this ?
            print "well done !"
            break
        elif time.time() > (timeout + start_time) :
            print "oh! to late, sorry !"
            break
        time.sleep(timeout/100)
thread.start_newthread(test_me,((func,args,timeout),))

important warnings : need use thread for Non-freezing application, got 3 thread for this: 1-main app, 2-test_me, 3- Your function(func)

Don't forget adding external variable to your function (for killing your function thread)

dsgdfg
  • 1,492
  • 11
  • 18