jeudi 6 août 2015

Load metadata for list of files asynchronous, while displayed in GUI

I am looking for advice on how to re-design my program. It is some sort of file browser. In the GUI, it displays a folder tree on the left side, and the elements of the currently selected folder on the right.

Each folder node and a file node have their own synchronization primitive and each element has a reload() method that loads the metadata about the file (version, thumbnail, etc.) and only synchronizes the when the final data has been loaded and is transferred to the objects attributes.

class FileNode(Synchronizable):
    def reload(self):
        with self.synchronized:
            self.status = STATUS_LOADING
            self.thumbnail = None
            # ...
        # Load the information ...
        thumbnail = load_thumbnail(self)
        with self.synchronized:
            self.status = STATUS_DONE
            self.thumbnail = thumbnail
            # ...

When a node should be displayed but is has been figured that it has not been loaded yet, it is added to a queue to be reloaded in a seperate thread.

for node in files_to_display:
    with node.synchronized:
        if node.status == STATUS_EMPTY:
            node.status = STATUS_LOADING
            queue_reload(node)

queue_reload() in turn also has a synchronization primitive since another thread consumes the items in the queue.

Now, in my case, I have a folder with approx. 1900 files that all haven't been loaded the first time they are to be displayed. It takes about 10 seconds to complete the above for-loop.

How can I improve the performance of my program? Are there efficient design patterns for such tasks?

Aucun commentaire:

Enregistrer un commentaire