Passing an Event from a Thread to its Caller

Mark Hammond mhammond at skippinet.com.au
Mon Jun 2 23:44:29 EDT 2003


Matt Rapoport wrote:

> Hi all, I'm a newbie to python and threading in general.
> 
> I'm trying to write a service using the win32 extensions.  My service
> runs a thread and the thread is just a simple class that I created.
> 
> I'd like that simple class of mine to be able to generate an event
> within the service.  How do I do that?  Thank you very much for the
> help.

Create the event, and pass it to the constructor of your thread, or even 
just set the attribute manually.  Eg:

class ClientThread(threading.Thread):
     def run(self):
         assert self.event is not None, "Why hasn't my event been set?"
         client = Client.Client()
         client.run()
	win32event.SetEvent(self.event)

...
     def SvcDoRun(self):
         client = ClientThread()
         client.event = self.hWaitStop
         client.setDaemon(1)
         client.start()
         win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

And it should set the event correctly.

In general, the whole point of an event is to be shared between threads, 
so there are no "philisophical" issues with passing them around, 
stashing them as module level globals, or whatever else works for you.

Mark.





More information about the Python-list mailing list