1 # -*- coding: utf-8 -*-
3 ###########################################################################
5 # Copyright (C) 2009 Joaquim Rocha <jrocha@igalia.com>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 ###########################################################################
21 from threading import Thread
25 logging.basicConfig(level=logging.DEBUG)
27 class AsyncItem(object):
29 def __init__(self, target_method, target_method_args, finish_callback=None, finish_callback_args=()):
30 self.target_method = target_method
31 self.target_method_args = target_method_args
32 self.finish_callback = finish_callback
33 self.finish_callback_args = finish_callback_args
39 results = error = None
41 results = self.target_method(*self.target_method_args)
42 except Exception, exception:
43 logging.debug(str(exception))
45 if self.canceled or not self.finish_callback:
47 self.finish_callback_args += (results,)
48 self.finish_callback_args += (error,)
49 gobject.idle_add(self.finish_callback, *self.finish_callback_args)
54 class AsyncWorker(Thread):
58 self.queue = Queue.Queue(0)
60 self.async_item = None
64 while not self.stopped:
65 if self.queue.empty():
69 self.async_item = self.queue.get()
72 self.queue.task_done()
73 self.async_item = None
74 except Exception, exception:
75 logging.debug(str(exception))
81 self.async_item.cancel()