I use callbacks from UrlRequest to set the status of the request accordingly. Is this what you mean? Sure, I want to resume the process as soon as the callback is done since that's what sets the return value, but I need to wait for the response from the callback in the meantime. How to wait for it is the question I´m asking, because any method I could try did not work in the scenario when the request returns an error.
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.network.urlrequest import UrlRequest
class WebExplorer():
def test_connection(self, path, cb):
self.path = path
UrlRequest(self.path, on_failure=cb,
on_error=cb, debug=True,
on_success=cb,
on_redirect=cb)
return self
class MainScreen(FloatLayout):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.URLtest = ['http://www.ikea.com/', 'https://www.google.com',
'https://www.sdfwrgaeh.com']
explorers = [] # In case you want to use them
for URL in self.URLtest:
explorers.append(WebExplorer().test_connection(URL,
self.got_result))
def got_result(self, *args):
print("got results {0}".format(args))
Thanks for the example. I don't understand everything so I have some research to do to figure out how your code works in details. Mainly how you can execute the call back of UrlRequest outside the WebExplorer class.
There's one caveat with this solution however, which is that it sorts of defeats the purpose of using the WebExplorer class as a one liner to check URL and then resume the main script. Using your solution you can only resume starting from a new callback function. The call to UrlRequest can only be the last command you run. So waiting for the request to complete is basically achieved by not having more code to execute in the main loop and resume once the call back function has been called. You effectively need one new callback function every time you want to call UrlRequest (assuming you can't always group all requests in one single call like you did in your example)
E.g. I cannot effectively design a call to UrlRequest handling all events in one line and carry on the execution on the next following line, like:
[stuff][call to UrlRequest, the class handles the waiting][more stuff, like checking the success of the above call]
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.network.urlrequest import UrlRequest
class MainScreen(FloatLayout):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.URLtest = ['http://www.ikea.com/', 'https://www.google.com',
'https://www.sdfwrgaeh.com']
for URL in self.URLtest:
UrlRequest(URL, on_failure=self.got_fail,
on_error=self.got_error, debug=True,
on_success=self.got_success,
on_redirect=self.got_redirect)
def got_success(self, req, *args):
print("got success {0}".format(req.url))
def got_error(self, req, *args):
print("got error {0}".format(req.url))
def got_fail(self, req, *args):
print("got fail {0}".format(req.url))
def got_redirect(self, req, *args):
print("got redirect {0}".format(req.url))
I appreciate the feedback, thanks. What you presented is the way I´ve been using UrlRequest earlier.I don't feel like I managed to present my point correctly, though, judging by your feedback. Apologies for that. I think I need to be more specific. Basically I want to be able to dig into a URL structure, with a root URL, folders underneath (several levels) and finally a list of files. To do that, I am initiating a first UrlRequest to the root to identify the folders. Etc. until I get the list of files I´m looking after, based on user UI input and so on. Therefore, I typically need to call UrlRequest, process the results, then call it again based on user input, and then possibly calling it again to "explore" the file structure. So the calls are not directly subsequent. My problem is not that I need to handle X calls simultaneously, it's that I need to handle X calls that require some processing between the calls.
My initial feeling based on that requirement (which I implemented, it's just that it doens't handle on_failure as it hangs on it, which is the object of me posting here), and given my background, is that I don't see myself create 5 functions in my App just to handle one call to UrlRequest. 4 callbacks, and 1 new function to carry on the main process. Then either from same function or another one, call UrlRequest again, fork into 4 new callbacks, and carry on in again a new function. That's a bit much for the same basic request?
UrlRequest(URL, on_failure=self.got_result,
on_error=self.got_result, debug=True,
on_success=self.got_result,
on_redirect=self.got_result)
UrlRequest(URL, on_failure=lambda *args: self.got_result("failed", *args),
lambda *args: self.got_result("success", *args)
That's why I wanted to design it as a class call to handle all the events, therefore streamlining my script by limiting the number of functions, however still preserving the event handling. But maybe this way of thinking is not compliant to the philosophy behind Kivy, or is technically not feasible?