Re: [pysystray-users] Threads and "global variable"
Status: Beta
Brought to you by:
essiene
From: Essien I. E. <es...@da...> - 2006-09-15 09:14:29
|
Christian Keydel wrote: > Hi Essien, > > Thanks for your response! > > <snip> > > that's the way I would do it :) > > Thanks again for the suggestion, but it raised more questions from a > n00b like me when it comes to Win32 programming. How do I open the > socket? How do I check if a message is waiting before I get it, in case > I need the thread to be always running? I actually tried to open a local > socket using 'localhost' and '127.0.0.1' and got a "connection refused" > error, so I gave up. > there are some socket examples in the std documentation, i learnt them from there meself :) > However, I found an (IMHO) easier option using a queue. Here is my solution: > > import Queue > ... > msg_queue = Queue.Queue() > ... > @systray.threaded > def my_lil_thread(): > new_value = 0 > while (1): > if not msg_queue.empty(): > new_value = msg_queue.get() > if new_value == 123 # Do whatever... > ... > > # Then in the callback for a menu item: > > msg_queue.put(123) # Cause my_lil_thread to do whatever > > nice... and simpler too :) > I believe it works because the thread "inherits" all global objects that > exist in the module at the time the thread is started (or defined, I > don't know) and because msg_queue is a reference that remains static > once the queue is opened, and the queue itself is a sharable object that > is accessible from all threads. It is probably not the right way to do > it and a Python expert might scream in horror but it works fine! :-) > scream in horror? hehe... good solution is one with less code, less mind bending, simple to understand. this is an elegant solution. Once more... my overtly complex mind overlooks simple solutions, but yay! for the n00b!!! ;) > Cheers, > Chris > > ------------------------------------------------------------------------- > Using Tomcat but need to do more? Need to support web services, security? > Get stuff done quickly with pre-integrated technology to make your job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > pysystray-users mailing list > pys...@li... > https://lists.sourceforge.net/lists/listinfo/pysystray-users > |