0

Consider following problem: I have a gtk / tk app which displays content from a website in a List(Store). I want to do the following things in order:

  • display the window & start downloading
  • show a progress bar
  • on completion of the downloads add the data into the list(Store)

This is the condition: the user has to be able to interact with the app while it is downloading. That means that the program is in the window's mainloop during the entire download.

What does not work:

  • urllib.urlopen() waits for the entire download to complete
  • Popen() does not allow the communication I want between the two threads

How to notify the program that the download has complete is the biggest question

Since I am event driven anyway because of Tk/Gtk I might as well use signals

My preferred way of solving this would be registering an additional signal "dl_done" and sending that signal to gtk when the download has finished. Is that even possible? Any suggestions are apreciated!

Azsgy
  • 3,139
  • 2
  • 29
  • 40

1 Answers1

1

A simple solution is:

  • to share a Queue object between the Gtk thread and the download thread
  • when a download is complete, you put the data in the queue (eg. a tuple with the download URL and the downloaded contents) from the download thread
  • in the Gtk thread, you set up a glib timer checking periodically if something new is in the queue (say, every 100 milliseconds for example) thanks to the "get_nowait" method of the Queue object.

You can have multiple download threads, if needed.

mguijarr
  • 7,641
  • 6
  • 45
  • 72
  • http://stackoverflow.com/questions/1052574/run-a-function-every-x-minutes-python and the queue documentation helped me, but it might be better to give a simple example. – Azsgy Sep 29 '13 at 18:02
  • So did you manage to do what you wanted, or do you need more help? – mguijarr Sep 30 '13 at 09:51
  • works :) I just thought it might be better to include a example for anyone else coming here :) – Azsgy Sep 30 '13 at 15:14