Search code examples
pythonpython-3.xconcurrent.futures

Track how long a thread spent actually working on a future


I would like to keep track of how long a thread in a ThreadPoolExecutor spend working on a piece of code that I submitted to the pool, not how long the work item spent sitting in the pool. I, of course, call future.result() to get the result, but I was hoping there was some way I could call future.time() or something like that to get the execution time. Any suggestions?


Solution

  • I would write a simple wrapper that does this:

    def timed(func):
        def _w(*a, **k):
            then = time.time()
            res = func(*a, **k)
            elapsed = time.time() - then
            return elapsed, res
        return _w
    

    Then you invoke your future e.g. with executor.map(timed(is_prime), PRIMES)) (example taken from the docs).

    Of course then you need to unpack the resulting timestamp & result

    elapsed, result = future.result()