On 2008-01-17 16:16, Dirk Meyer wrote:
> 3. Rename Thread to ThreadCallback and rename start to __call__. A new
>    Thread class will inherit from ThreadCallback and add a deprected
>    warning and this functions also has a start function with a
>    deprected warning.
>   

Don't just rename it; __call__ must accept arguments, whereas start()
doesn't.  In fact, ThreadCallback shouldn't derive from threading.Thread
anymore because the idea is that new threads are spawned each time the
ThreadCallback object is called.

See the attached, which implements it the way I think it should be
(roughly).  I think this is an elegant implementation.  I didn't inherit
ThreadCallback from your BackgroundTask class because I couldn't get it
to work.  You'll see what I mean when I try to make it go. :)


> 6. Add support for foo = yield something in kaa.base. This will
>    require some Python 2.4/2.5 checking because I'm not sure if Freevo
>    1.8 is not using kaa.rpc which uses the decorator stuff. All code
>    in kaa.base will use the old style code to get the yield
>    results. The same will be true for kaa.imlib2 and kaa.metadata but
>    I guess they don't use it.
>   

I started working on this.  If you want to hold off on this one, I'll
try to finish it up on Sunday or tomorrow if I have time. (I have family
visiting this weekend.)

Jason.
import kaa
import threading
import time

def threaded_func(*args, **kwargs):
    print "This is running in a thread", args, kwargs, " -- sleeping for 2 seconds."
    time.sleep(2)
    return 42

def handle_result(result):
    print "Function returned with", result
    print "Is mainthread?", kaa.is_mainthread()
    raise SystemExit

class _CallableThread(threading.Thread, kaa.InProgress):
    def __init__(self, callback):
        super(_CallableThread, self).__init__()
        super(kaa.InProgress, self).__init__()
        
        self._callback = callback

    def __call__(self, *args, **kwargs):
        self._wrapped_callback = kaa.Callback(self._callback, *args, **kwargs)
        self.start()
        return self

    def run(self):
        result = self._wrapped_callback()
        # TODO: handle exception
        kaa.MainThreadCallback(self.finished)(result)


class ThreadCallback(kaa.Callback):
    def __init__(self, callback, *args, **kwargs):
        kaa.Callback.__init__(self, callback, *args, **kwargs)

    def _get_callback(self):
        return _CallableThread(super(ThreadCallback, self)._get_callback())

    def __call__(self, *args, **kwargs):
        return super(ThreadCallback, self).__call__(*args, **kwargs)


def spew():
    print "Loop is still running ..."

kaa.Timer(spew).start(0.2)
t = ThreadCallback(threaded_func, 'hello', foo = 42)
t().connect(handle_result)
kaa.main.run()
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Freevo-devel mailing list
Freevo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/freevo-devel

Reply via email to