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

风格不统一 提交于 2019-12-12 08:07:46

问题


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.


回答1:


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.




回答2:


I do not know much about concurrent.futures, but you can use this logic to break the time. Use a loop instead of sleep.time() or wait()

for i in range(sec):
    sleep(1)

interrupt or break can be used to come out of loop.



来源:https://stackoverflow.com/questions/38461603/how-to-break-time-sleep-in-a-python-concurrent-futures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!