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

Discussion in 'Python' started by grocery_stocker, Apr 10, 2009.

  1. 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
     
    grocery_stocker, Apr 10, 2009
    #1
    1. Advertising

  2. Re: How come I can't get get my background thread to output anything?

    On Apr 9, 6:36 pm, grocery_stocker <> 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
     
    grocery_stocker, Apr 10, 2009
    #2
    1. Advertising

  3. Re: How come I can't get get my background thread to output anything?

    On Apr 9, 7:01 pm, grocery_stocker <> wrote:
    > On Apr 9, 6:36 pm, grocery_stocker <> 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 1:pass like in the following

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

    the code works as expected
     
    grocery_stocker, Apr 10, 2009
    #3
  4. grocery_stocker

    Carl Banks Guest

    Re: How come I can't get get my background thread to output anything?

    On Apr 9, 8:36 pm, grocery_stocker <> wrote:
    > On Apr 9, 7:01 pm, grocery_stocker <> wrote:
    >
    >
    >
    > > On Apr 9, 6:36 pm, grocery_stocker <> 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 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
     
    Carl Banks, Apr 10, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    1,974
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
  2. John Benson
    Replies:
    1
    Views:
    357
    Hoang
    Jan 15, 2004
  3. mason66
    Replies:
    0
    Views:
    430
    mason66
    Jul 27, 2006
  4. JWL
    Replies:
    4
    Views:
    596
    Michael Winter
    Sep 26, 2006
  5. charles
    Replies:
    4
    Views:
    427
Loading...

Share This Page