Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement