Advertisement
Guest User

Untitled

a guest
May 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import futurist
  2. import futurist.waiters
  3. import six
  4. import subprocess
  5. import sys
  6. import time
  7.  
  8. EXECUTOR = futurist.ThreadPoolExecutor()
  9. TIMEOUT = 5
  10. WORK_AMOUNT = 10
  11.  
  12. def cancel():
  13. print('\ncancel succeeded:')
  14. print(future.cancel())
  15. print(EXECUTOR.statistics)
  16.  
  17. def cb(*args, **kwargs):
  18. print('done callback called, but thread remains running (check with ps -T)')
  19. subprocess.run(['ps', '-Tf', '-C', 'python3'])
  20. cancel()
  21.  
  22. def work(*args, **kwargs):
  23. time.sleep(WORK_AMOUNT)
  24. #sys.exit(1)
  25. return 42
  26.  
  27. try:
  28. future = EXECUTOR.submit(work)
  29. future.add_done_callback(cb)
  30. # Can't be cancelled when running or done (c)
  31. cancel()
  32. # Raises TimeoutError, but never CancelledError :(
  33. # returns no result here, when TIMEOUT is shorter than WORK_AMOUNT
  34. print('Result: %s' % future.result(TIMEOUT))
  35. except Exception as e:
  36. print('caught exception %s' % six.text_type(e))
  37. # And can never be cancelled...
  38. cancel()
  39. #raise e
  40.  
  41. while True:
  42. print(futurist.waiters.wait_for_all([future], timeout=1))
  43. if not future.done():
  44. print(EXECUTOR.statistics)
  45. cancel()
  46. else:
  47. print(future.result())
  48. print('done, but thread remains running')
  49. break
  50.  
  51. time.sleep(3)
  52. # never...
  53. cancel()
  54. print('thread remains running (check with ps -T)')
  55. subprocess.run(['ps', '-Tf', '-C', 'python3'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement