Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How come I get get my background thread to output anything?

Reply
Thread Tools

How come I get get my background thread to output anything?

 
 
grocery_stocker
Guest
Posts: n/a
 
      04-10-2009
Given the following....

#!/usr/local/bin/python

import os
import time
import thread

def domsg(string, sleeptime, *args):
print "%s was here", string
#os.system('tel %s test' % person);
#time.sleep(sleeptime)

def buildlist():
out = 1
persons = []

while(out != 0):
pern = raw_input("Enter person to message:")
if (len(pern)):
persons.append(pern)
else:
out = 0
return persons

if __name__ == "__main__":
#buildlist()
thread.start_new_thread(domsg, ("person",2))

I get....
m-net% ./massmsg.py
m-net%

I was expecting to see

person was here
 
Reply With Quote
 
 
 
 
grocery_stocker
Guest
Posts: n/a
 
      04-10-2009
On Apr 9, 6:36 pm, grocery_stocker <(E-Mail Removed)> wrote:
> Given the following....
>
> #!/usr/local/bin/python
>
> import os
> import time
> import thread
>
> def domsg(string, sleeptime, *args):
> print "%s was here", string
> #os.system('tel %s test' % person);
> #time.sleep(sleeptime)
>
> def buildlist():
> out = 1
> persons = []
>
> while(out != 0):
> pern = raw_input("Enter person to message:")
> if (len(pern)):
> persons.append(pern)
> else:
> out = 0
> return persons
>
> if __name__ == "__main__":
> #buildlist()
> thread.start_new_thread(domsg, ("person",2))
>
> I get....
> m-net% ./massmsg.py
> m-net%
>
> I was expecting to see
>
> person was here


 
Reply With Quote
 
 
 
 
grocery_stocker
Guest
Posts: n/a
 
      04-10-2009
On Apr 9, 7:01 pm, grocery_stocker <(E-Mail Removed)> wrote:
> On Apr 9, 6:36 pm, grocery_stocker <(E-Mail Removed)> wrote:
>
> > Given the following....

>
> > #!/usr/local/bin/python

>
> > import os
> > import time
> > import thread

>
> > def domsg(string, sleeptime, *args):
> > print "%s was here", string
> > #os.system('tel %s test' % person);
> > #time.sleep(sleeptime)

>
> > def buildlist():
> > out = 1
> > persons = []

>
> > while(out != 0):
> > pern = raw_input("Enter person to message:")
> > if (len(pern)):
> > persons.append(pern)
> > else:
> > out = 0
> > return persons

>
> > if __name__ == "__main__":
> > #buildlist()
> > thread.start_new_thread(domsg, ("person",2))

>
> > I get....
> > m-net% ./massmsg.py
> > m-net%

>
> > I was expecting to see

>
> > person was here


Never mind. When i add while 1ass like in the following

thread.start_new_thread(domsg, ("person",2))
while 1 : pass

the code works as expected
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      04-10-2009
On Apr 9, 8:36*pm, grocery_stocker <(E-Mail Removed)> wrote:
> On Apr 9, 7:01 pm, grocery_stocker <(E-Mail Removed)> wrote:
>
>
>
> > On Apr 9, 6:36 pm, grocery_stocker <(E-Mail Removed)> wrote:

>
> > > Given the following....

>
> > > #!/usr/local/bin/python

>
> > > import os
> > > import time
> > > import thread

>
> > > def domsg(string, sleeptime, *args):
> > > * * print "%s was here", string
> > > * * #os.system('tel %s test' % person);
> > > * * #time.sleep(sleeptime)

>
> > > def buildlist():
> > > * * out = 1
> > > * * persons = []

>
> > > * * while(out != 0):
> > > * * * * pern = raw_input("Enter person to message:")
> > > * * * * if (len(pern)):
> > > * * * * * * persons.append(pern)
> > > * * * * else:
> > > * * * * * * out = 0
> > > * * return persons

>
> > > if __name__ == "__main__":
> > > * * #buildlist()
> > > * * thread.start_new_thread(domsg, ("person",2))

>
> > > I get....
> > > m-net% ./massmsg.py
> > > m-net%

>
> > > I was expecting to see

>
> > > person *was here

>
> *Never mind. When i add while 1ass like in the following
>
> thread.start_new_thread(domsg, ("person",2))
> while 1 : pass
>
> the code works as expected


Whoa, there, chief, you don't want to do that. It'll cause a busy
loop and run one of your CPUs to 100%.

Instead, use the theading module and the join method:

import threading
thr = threading.Thread(target=domsg,args=("person",2))
thr.start()

# do whatever in the main thread

thr.join()


Carl Banks
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is it ok to run another thread inside the page then come back charles ASP .Net 4 02-22-2007 10:25 PM
Thread was being aborted thrown for background thread (win2003 ser =?Utf-8?B?Sm9oYW5uYQ==?= ASP .Net 3 10-15-2004 01:35 PM
the thread that refused to die: I come not to bury C++,but to praise it... John Benson Python 1 01-15-2004 10:17 PM
Come One, Come All Jess Guim Digital Photography 0 12-02-2003 04:43 PM



Advertisments