I am using python with gtk.
I want to download a file, and while this is happening i draw an animated gif on the window.
But everytime the download starts, the gif is freezing. I thought the gobject call should fix this, but seems not like.
The call is:
in the gui class
...
gobject.timeout_add(3000, self.load)
gtk.main()
and the load function:
def load(self):
ul = urllib2.open('http://xxxx/')
data = ul.read()
while gtk.events_pending():
gtk.main_iteration()
return True
with every call of load the gui stacks. Any way to do that better?
original code:
self.opener = urllib2.build_opener()
self.opener.addheaders.append(('Cookie', self.cookie))
self.state = self.opener.open('http://'+gd_adress+'/state.cst?Lang=en')
self.state_data = self.state.read()
You need to use an asynchronous call that integrates with the GObject main loop.
Probably the easiest is to use GIO:
Jono Bacon has a great writeup: http://www.jonobacon.org/2010/03/15/download-files-async-with-gio-and-python/
Unfortunately GIO doesn’t support setting HTTP cookies as far as I can tell. In that case the best option is probably to use threads and use
GLib.idle_addto return data to the main loop, as in Webkit threads with PyGObject on Gtk3:The idea is that this encapsulates the blocking call in a thread that returns data to the main thread when it is ready, so the rest of your code can ignore the fact that threads are being used.