|
|
|
|
|
|
| Author |
Message |
gel *nix forums beginner
Joined: 05 Jul 2006
Posts: 13
|
Posted: Thu Jul 20, 2006 2:08 am Post subject:
New to threads. How do they work?
|
|
|
Hi all
I am attempting to understand threads to use in a network app which I
am writing. The part that I am going to use threads on is run on the
clients/workstations. I will monitor all starting and ending
processes. Below is what I have been doing. It looks like only the
first thread is starting. Can someone explain a basic example of how
threads work and are implemented, what is better to use threading or
thread module, and what am I doing wrong in the example below. I know
I have asked a lot of questions, any help/direction would be
appreciated.
Thanks
import wmi
import pythoncom
import thread
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()
thread.start_new_thread(create(),())
thread.start_new_thread(delete(),()) |
|
| Back to top |
|
 |
Dennis Lee Bieber *nix forums Guru
Joined: 05 Mar 2005
Posts: 819
|
Posted: Thu Jul 20, 2006 5:43 am Post subject:
Re: New to threads. How do they work?
|
|
|
On 19 Jul 2006 19:08:12 -0700, "gel" <geli@tasmail.com> declaimed the
following in comp.lang.python:
| Quote: | import thread
Step one... Skip the thread module and use threading module instead. |
| Quote: | 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...
| Quote: |
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@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/ |
|
| Back to top |
|
 |
gel *nix forums beginner
Joined: 05 Jul 2006
Posts: 13
|
Posted: Thu Jul 20, 2006 6:53 am Post subject:
Re: New to threads. How do they work?
|
|
|
Dennis Lee Bieber wrote:
| Quote: | On 19 Jul 2006 19:08:12 -0700, "gel" <geli@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@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@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 |
|
| Back to top |
|
 |
Dennis Lee Bieber *nix forums Guru
Joined: 05 Mar 2005
Posts: 819
|
Posted: Thu Jul 20, 2006 5:20 pm Post subject:
Re: New to threads. How do they work?
|
|
|
On 19 Jul 2006 23:53:22 -0700, "gel" <geli@tasmail.com> declaimed the
following in comp.lang.python:
| Quote: | working so far. And another question why do you prefer to us threading
and thread?
As the documentation states, thread is a "low-level" method, and |
only supplies a basic lock for synchronization.
threading is a higher-level interface, with more synchronization
capability...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/ |
|
| Back to top |
|
 |
Lawrence D'Oliveiro *nix forums Guru
Joined: 25 Mar 2005
Posts: 723
|
Posted: Fri Jul 21, 2006 10:41 am Post subject:
Re: New to threads. How do they work?
|
|
|
In message <1153361291.923692.45330@s13g2000cwa.googlegroups.com>, gel
wrote:
| Quote: | I am attempting to understand threads to use in a network app which I
am writing.
|
It is written, somewhere in the philosophy of *nix programming, that threads
are a performance hack, to be avoided wherever possible. Use processes in
preference to threads. |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Sun Nov 23, 2008 2:14 pm | All times are GMT
|
|
Skateboarding | Credit Reports | Credit Card Consolidation | Loans | WoW Gold
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|