Wednesday, August 24, 2005

Threaded handlers in Pysystray

One of the important TODO's I have for pysystray is making the menu item callbacks/handlers threaded. To acheive this, I thought of the simlplest most unobstrusive way to add it, and I decided to go with Decorators.
 
Uptill yesternite, I didn't really get Decorators, but back at home, Stan (my flat mate) and I took a look at the PEP, and suddently It was all clear.
 
I then wrote this simple decorator:
 
>>> import thread
>>>
>>> def run_threaded(func):
>>>    def wrapper(*args):
>>>        thread.start_new_thread(func, tuple(args))
>>>    return wrapper
 
 
To run any function in a seperate thread of execution you'd do this:
 
>>> import time #just for a sleeping example :)
 
>>> @run_threaded
>>> def looper(x):
>>>     for i in xrange(0, x): #gotta love generators :)
>>>         print i
>>>        time.sleep(2)
 
now running:
 
>>> looper(10)
 
will run it in a seperate thread of execution.
 
With me finally understanding Decorators, I now have just metaclasses left to understand before I can say I'm trully upto step on python 2.4's features. But man... you just gotta love this language... Its sooooo malleable.
 
I've added the run_threaded decorator to pysystray, but i've not released this version yet... I'm going to refactor the files a bit more, and try to attack one or two more todo's before i release 0.5.0.
 
Btw, If you're wondering why this is an important addition... Currently, if a MenuItem callback/handler takes too long to complete, it hangs the UI, and the pop-up menu no more shows up. This will also be helpfull for the on_load event handler so it also doesn't lock up the UI.
 
Oh.. btw, I'm thinking of using the Google API message notifier for pysystray's notifier... If its possible. I'll start groking the SDK next :)

1 Comments:

At Wednesday, August 24, 2005 1:48:00 PM, Blogger Mfoniso said...

Yeah, I did stumble on google talk this morning though I haven't had opportunity to try it out. But I was sure sure it rocked, just like everything from Google.

You say they've got the world by the... :O No wonder! I've had this terrible ache down there for the past few weeks. Doctors couldn't find the problem. I'm suing google!

Oh well, i guess I can only just be envious till I have my pet machine hooked up to the net. For now I'll just have to live withthe constraints of my school network! :( No Google-talking for me... hmmm... maybe I can tweak the network a little to my advantage. Muhahaha... Cya'll in gtalk

 

Post a Comment

<< Home