~tony-badwolf/quickly/threaded_application

« back to all changes in this revision

Viewing changes to data/templates/threaded-ubuntu-application/project_root/python/helpers.py

  • Committer: tony
  • Date: 2010-09-16 19:55:40 UTC
  • Revision ID: tony@tony-laptop-20100916195540-i9lgho9h5m9bjl8b
Threads are useful in gui applications. Without them slow processes freeze the GUI. This uses the node concept from erlang (quickly uses couchdb, couchdb uses erlang, I wondered why). It is implemented using python's Queue module. There is a bug catcher for exceptions in other threads. Contains example code to drive a progress bar, disable buttons when busy and show exception in threaded model. Demo code is clearly marked and can be removed to make a clean project. All threading code is "hidden" in helpers.py. Implemented as new template based on ubuntu_application.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
### BEGIN LICENSE
 
3
# This file is in the public domain
 
4
### END LICENSE
 
5
 
 
6
"""Helpers for an Ubuntu application."""
 
7
 
 
8
__all__ = [
 
9
    'make_window',
 
10
    ]
 
11
 
 
12
import os
 
13
import gtk
 
14
 
 
15
import threading
 
16
import Queue
 
17
import traceback
 
18
 
 
19
from python_name.python_nameconfig import get_data_file
 
20
 
 
21
import gettext
 
22
from gettext import gettext as _
 
23
gettext.textdomain('project_name')
 
24
 
 
25
def get_builder(builder_file_name):
 
26
    """Return a fully-instantiated gtk.Builder instance from specified ui 
 
27
    file
 
28
    
 
29
    :param builder_file_name: The name of the builder file, without extension.
 
30
        Assumed to be in the 'ui' directory under the data path.
 
31
    """
 
32
    # Look for the ui file that describes the user interface.
 
33
    ui_filename = get_data_file('ui', '%s.ui' % (builder_file_name,))
 
34
    if not os.path.exists(ui_filename):
 
35
        ui_filename = None
 
36
 
 
37
    builder = gtk.Builder()
 
38
    builder.set_translation_domain('project_name')
 
39
    builder.add_from_file(ui_filename)
 
40
    return builder
 
41
 
 
42
 
 
43
# Owais Lone : To get quick access to icons and stuff.
 
44
def get_media_file(media_file_name):
 
45
    media_filename = get_data_file('media', '%s' % (media_file_name,))
 
46
    if not os.path.exists(media_filename):
 
47
        media_filename = None
 
48
 
 
49
    return "file:///"+media_filename
 
50
 
 
51
class Taskqueue:
 
52
    def __init__(self):        
 
53
        self._q = Queue.Queue()
 
54
        t = threading.Thread(target=self._do_task)
 
55
        t.setDaemon(True)
 
56
        t.start()
 
57
 
 
58
    def add_task(self, task, name, reply, data = {}):
 
59
        # incoming tasks - put into model loop
 
60
        self._q.put([task, name, reply, data])
 
61
 
 
62
    def _do_task(self):
 
63
        # this loop is outside of the gtk.main loop
 
64
        while True:
 
65
            task, name, reply, data = self._q.get()
 
66
            try:
 
67
                task(name, reply, data) # call a model function or method
 
68
            except Exception, e:
 
69
                error_in_thread(task, name, reply, data)
 
70
            self._q.task_done()
 
71
 
 
72
def error_in_thread(task, name, reply, data):
 
73
    response = {}
 
74
    response['reason'] = traceback.format_exc()
 
75
    response['task'] = task
 
76
    response['data'] = data
 
77
    reply(name, 'error', response)
 
78