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=[] ): 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)
MyThread().start()
This is one of the best uses I have seen for a nested class definition.
Suggestions from hardcore pythonistas on how to my make run_in_thread function more elegant are quite welcome also :)
I shortened it, at least. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list