Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break time.sleep() in a python concurrent.futures

I am playing around with concurrent.futures.

Currently my future calls time.sleep(secs).

It seems that Future.cancel() does less than I thought.

If the future is already executing, then time.sleep() does not get cancel by it.

The same for the timeout parameter for wait(). It does not cancel my time.sleep().

How to cancel time.sleep() which gets executed in a concurrent.futures?

For testing I use the ThreadPoolExecutor.

like image 227
guettli Avatar asked Jul 19 '16 14:07

guettli


1 Answers

If you submit a function to a ThreadPoolExecutor, the executor will run the function in a thread and store its return value in the Future object. Since the number of concurrent threads is limited, you have the option to cancel the pending execution of a future, but once control in the worker thread has been passed to the callable, there's no way to stop execution.

Consider this code:

import concurrent.futures as f
import time

T = f.ThreadPoolExecutor(1) # Run at most one function concurrently
def block5():
    time.sleep(5)
    return 1
q = T.submit(block5)
m = T.submit(block5)

print q.cancel()  # Will fail, because q is already running
print m.cancel()  # Will work, because q is blocking the only thread, so m is still queued

In general, whenever you want to have something cancellable you yourself are responsible for making sure that it is.

There are some off-the-shelf options available though. E.g., consider using asyncio, they also have an example using sleep. The concept circumvents the issue by, whenever any potentially blocking operation is to be called, instead returning control to a control loop running in the outer-most context, together with a note that execution should be continued whenever the result is available - or, in your case, after n seconds have passed.

like image 82
Phillip Avatar answered Nov 15 '22 12:11

Phillip