niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
New to threads. How do they work?
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
Author Message
Lawrence D'Oliveiro
*nix forums Guru


Joined: 25 Mar 2005
Posts: 723

PostPosted: Fri Jul 21, 2006 10:41 am    Post subject: Re: New to threads. How do they work? Reply with quote

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
Dennis Lee Bieber
*nix forums Guru


Joined: 05 Mar 2005
Posts: 819

PostPosted: Thu Jul 20, 2006 5:20 pm    Post subject: Re: New to threads. How do they work? Reply with quote

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
gel
*nix forums beginner


Joined: 05 Jul 2006
Posts: 13

PostPosted: Thu Jul 20, 2006 6:53 am    Post subject: Re: New to threads. How do they work? Reply with quote

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

PostPosted: Thu Jul 20, 2006 5:43 am    Post subject: Re: New to threads. How do they work? Reply with quote

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

PostPosted: Thu Jul 20, 2006 2:08 am    Post subject: New to threads. How do they work? Reply with quote

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
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [5 Posts] View previous topic :: View next topic
The time now is Thu Dec 04, 2008 4:29 am | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Need help getting Sony DAT tape drive to work on Irix 6.5 trebor SGI/IRIX 1 Sun Apr 13, 2008 3:19 am
No new posts jinitiator - how do versions work ? Grumps Oracle 0 Fri Jul 21, 2006 9:05 am
No new posts Work-needing packages report for Jul 21, 2006 wnpp@debian.org devel 0 Fri Jul 21, 2006 6:50 am
No new posts Can anybody help me get a job or work robic0@nirgendwo Perl 5 Fri Jul 21, 2006 5:42 am
No new posts Reading long lines doesn't work in Python Scott Simpson python 2 Wed Jul 19, 2006 4:25 pm

Property for sale in Spain | Credit Check | MPAA | Online Gift Shopping | Ringtones
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
[ Time: 0.1744s ][ Queries: 20 (0.0867s) ][ GZIP on - Debug on ]