0

I have some object, that can perform operations that take a lot of time. I need some way to interrupt it after a some timeout. My code looks like this:

def f(obj):
    time.sleep(600)
    obj.cancel()
t = threading.Thread(target=f, args=(obj,))
t.start()
result = obj.ExecuteLongCommand(arguments)
t.stop() # Oops!

So, is there an [easy] way to perform async cancelable operation? My thread t is async, but is not cancelable.

SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
Jury
  • 1,227
  • 3
  • 17
  • 30
  • 2
    Possible solition: http://stackoverflow.com/questions/18018033/how-to-stop-a-looping-thread-in-python – qwetty Oct 01 '15 at 08:20
  • 1
    are you in control of the implementation of ExecuteLongCommand? – user1514631 Oct 01 '15 at 08:31
  • No, it is third-party module – Jury Oct 01 '15 at 08:40
  • @qwetty, as I see, I need to define a new `stoppable` implementation for `Thread`. Seems working, but I surprised, that there is no `build-in` option. – Jury Oct 01 '15 at 08:54
  • @Jury because it's bad practice :) Thread could run other threads, allocate some resources and they should finished/released normally not by thread killing. Maybe it's worth to make some research and find way to accomplish the same thing but in different way. – qwetty Oct 01 '15 at 09:32

0 Answers0