Created
May 9, 2019 14:44
-
-
Save bogdando/830d8e7e5cabcee375e0e9e885b9705c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import futurist | |
import futurist.waiters | |
import six | |
import subprocess | |
import sys | |
import time | |
EXECUTOR = futurist.ThreadPoolExecutor() | |
TIMEOUT = 5 | |
WORK_AMOUNT = 10 | |
def cancel(): | |
print('\ncancel succeeded:') | |
print(future.cancel()) | |
print(EXECUTOR.statistics) | |
def cb(*args, **kwargs): | |
print('done callback called, but thread remains running (check with ps -T)') | |
subprocess.run(['ps', '-Tf', '-C', 'python3']) | |
cancel() | |
def work(*args, **kwargs): | |
time.sleep(WORK_AMOUNT) | |
#sys.exit(1) | |
return 42 | |
try: | |
future = EXECUTOR.submit(work) | |
future.add_done_callback(cb) | |
# Can't be cancelled when running or done (c) | |
cancel() | |
# Raises TimeoutError, but never CancelledError :( | |
# returns no result here, when TIMEOUT is shorter than WORK_AMOUNT | |
print('Result: %s' % future.result(TIMEOUT)) | |
except Exception as e: | |
print('caught exception %s' % six.text_type(e)) | |
# And can never be cancelled... | |
cancel() | |
#raise e | |
while True: | |
print(futurist.waiters.wait_for_all([future], timeout=1)) | |
if not future.done(): | |
print(EXECUTOR.statistics) | |
cancel() | |
else: | |
print(future.result()) | |
print('done, but thread remains running') | |
break | |
time.sleep(3) | |
# never... | |
cancel() | |
print('thread remains running (check with ps -T)') | |
subprocess.run(['ps', '-Tf', '-C', 'python3']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment