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

G

grocery_stocker

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
 
G

grocery_stocker

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
 
G

grocery_stocker

Given the following....

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 1:pass like in the following

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

the code works as expected
 
C

Carl Banks

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 1:pass 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top