Mikhail Gerasimov
Mikhail Gerasimov

Reputation: 39606

Is there any way to cancel UrlRequest in Kivy?

Is there any way to cancel UrlRequest in Kivy?

def got_url(req, result):
    print(result)
req = UrlRequest('http://httpbin.org/delay/2', got_url)  # Request lasts 2 seconds

def my_callback(dt):
    print('Request cancelled.')
    # sort of req.cancel()
Clock.schedule_once(my_callback, 1)  # But some event happens after 1 sec. and I want to cancel request

This is just example: I know about timeout, I want to cancel request on some arbitrary event.

Upvotes: 2

Views: 300

Answers (1)

Peter Badida
Peter Badida

Reputation: 12199

Afaik there's no other way except UrlRequest.timeout, which could translate to politely wait and close any harmful stuff safely. It uses Thread which may and may not be dangerous. Even more if e.g. packaged into exe or other form of binary where it could create a lock because something broke. I think the way you would like it to use would only trigger problems.

There's another way using on_* events and as small as possible timeout, which can trigger your function.

Example: set timeout for 1s if you want to cancel it after that amount of time and let the UrlRequest ping you when it does so, which is

  1. safer to use than stopping it at random place
  2. less amount of lines for reinventing the wheel

Upvotes: 1

Related Questions