On Mon, 07 Dec 2009 18:21:23 -0000, Terry Reedy <tjre...@udel.edu> wrote:

r0g wrote:

The trick to threads is to create a subclass of threading.Thread, define
the 'run' function and call the 'start()' method. I find threading quite
generally useful so I created this simple generic function for running
things in threads...

Great idea. Thanks for posting this.

def run_in_thread( func, func_args=[], callback=None, callback_args=[] ):

I'm might wary of having mutable defaults for parameters. They make for the most annoying errors. Even though they're actually safe here, I'd still write:

def run_in_thread(func, func_args=(), callback=None, callback_args=()):

out of sheer paranoia.

    import threading
    class MyThread ( threading.Thread ):
       def run ( self ):
             # Call function
            if function_args:
                result = function(*function_args)
            else:
                result = function()

The check is not necessary. by design, f(*[]) == f()
Names do not match param names ;=)

             # Call callback
            if callback:
                if callback_args:
                    callback(result, *callback_args)
                else:
                    callback(result)

Ditto. g(x,*[]) == g(x)

     def run(self):
       result = func(*func_args) # matching run_in_thread param names
       callback(result, *callback_args)
Neat, but I think you mean

         if callback is not None:
             callback(result, *callback_args)

for that last line.


    MyThread().start()

This is one of the best uses I have seen for a nested class definition.
Ditto!

--
Rhodri James *-* Wildebeest Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to