New to threads. How do they work?

gel geli at tasmail.com
Thu Jul 20 02:53:22 EDT 2006


Dennis Lee Bieber wrote:

> On 19 Jul 2006 19:08:12 -0700, "gel" <geli at tasmail.com> declaimed the
> following in comp.lang.python:
>
> > import thread
> >
> 	Step one... Skip the thread module and use threading module instead.
>
> > def create():
> >
> >         pythoncom.CoInitialize()
> >         c = wmi.WMI()
> >         while 1 :
> >
> >             print "watching creation"
> >             watcher = c.watch_for(notification_type="Creation",
> > wmi_class="Win32_Process", delay_secs=1)
>
> 	I don't know WMI, but is that delay a real sleep operation, or a
> polling loop?
>
> 	And either could be a problem if they hold the GIL -- preventing
> anything else from running until they return...
>
> >
> > thread.start_new_thread(create(),())
> > thread.start_new_thread(delete(),())
>
> 	At the very least, I suggest commenting out the COM and WMI calls --
> test threads that ONLY print output and do a time.sleep(1). That should
> be sufficient to see if the threads themselves are being started.
> --
> 	Wulfraed	Dennis Lee Bieber		KD6MOG
> 	wlfraed at ix.netcom.com		wulfraed at bestiaria.com
> 		HTTP://wlfraed.home.netcom.com/
> 	(Bestiaria Support Staff:		web-asst at bestiaria.com)
> 		HTTP://www.bestiaria.com/

Thanks alot for your help.  I had tried using threading with a
different setup in the function side but did not get success. I think
that I have winner now.  Thanks again.  What follows is the what I have
working so far.  And another question why do you prefer to us threading
and thread?

import wmi
import pythoncom
import threading

def create():

        pythoncom.CoInitialize()
        c = wmi.WMI()
        while 1 :

            print "watching creation"
            watcher = c.watch_for(notification_type="Creation",
wmi_class="Win32_Process", delay_secs=1)
            print watcher()

def delete():

        pythoncom.CoInitialize()
        d = wmi.WMI()
        while 1 :
            print "watching deletion"
            watcher = d.watch_for(notification_type="Deletion",
wmi_class="Win32_Process", delay_secs=1)
            print watcher()

import threading
threading.Thread(target=create).start()
threading.Thread(target=delete).start()

Cheers




More information about the Python-list mailing list