[python-win32] Regarding Threads and locals()

Mike Driscoll mdriscoll at co.marshall.ia.us
Thu Mar 20 15:13:38 CET 2008


 


> Message: 6
> Date: Thu, 20 Mar 2008 16:07:15 +0530
> From: "Tejovathi P" <tejovathi.p at gmail.com>
> Subject: [python-win32] Regarding Threads and locals()
> To: python-win32 at python.org, py2exe-users at lists.sourceforge.net
> Message-ID:
> 	<aefc0fa70803200337l60d18f2bj3422e19ae95de1a8 at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hi all,
> 
> I have a GUI applicaion(along with threads). When the run 
> button is pressed
> in the GUI a separate thread starts( Thread is created using 
> beginthreadex)
> and does the
> required activity.
> Now, while the thread is being executed, i want the locals() 
> present inside
> the thread's run function to be avaialbe in the GUI class 
> from where the
> thread class is being created
> 
> EG:
> 
> *main.py*
> 
> class WorkerThread(threading.Thread):
> 
>     def __init__(self, ):
>          threading.Thread.__init__(self)
>          # Start the thread and invoke the run method
>          self.start()
> 
>     def run(self):
>           # Start the thread. It executed self.func() as a 
> separate thread
>           self.workerThread, tid = 
> win32process.beginthreadex(None, 0 ,
> self.func ,(), 1)
>           .......
> 
>      def func(self):
>            execfile(temp.py)
> 
> class GUI(wxFrame):
>        def __init__(self):
>            .....
>            .....
>        def  CreateThread(self):
> 
>              self.workerThread = WorkerThread()
> 
> 
> if name == _main_:
>         .....
> .        ....
> .        .....
> 
> *temp.py*
> 
>   i = 1   j = 2
>   k = 4
>   while(10000):
>         print i
>         print j
>         print k
>         i = 1+1
>         j = j+2
>         k = k + 3
> 
> 
> Now, while the thread is executin func and printing i, j, k , 
> In the main
> GUI thread how do i get the values of i, j ,k
> I tried with sys.modules, sys._current_frames, vars(). But 
> nothing worked
> out.
> 
> Ideally the locals() of func() should be passed to the GUI 
> thread, how?


One of the recommended ways of doing this is to have the thread write that
information to a file either send an event to the GUI that there's data
there or use a timer in the GUI event loop that looks for that file. I'm
actually messing with Tim Golden's watch_directory.py file for a VPN
project I'm working on in which my wxPython GUI app watches a file for
changes and then reads it.

You can check out Golden's work here:
http://tgolden.sc.sabren.com/python/win32_how_do_i/watch_directory_for_cha
nges.html

The Python docs talk about using the threading module's event object for
basic communication, which is what I was talking about: 

http://docs.python.org/lib/event-objects.html

In wxPython, I can wait for a threading event and then use a wx.PostEvent
to let my GUI know that the thread had an event. I'm sure there are
similar methods in Tkinter and pyGTK or whatever you're using.

Mike


More information about the python-win32 mailing list