Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8521179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:44:49+00:00 2026-06-11T06:44:49+00:00

I have a function foo that only stops once a condition has been met.

  • 0

I have a function foo that only stops once a condition has been met. While foo is running, I need to ask for user input the whole time (keeps asking for user input). I want them to run separately without interfering with each other.

In my example below, foo keeps printing ‘Hello’ and getUserInput keeps looking for user input. I want foo to keep printing hello even if i do not enter anything for user input. It will keep asking for input as long as the user does not enter letter ‘e’. I have my attempt below:

import threading
from time import sleep

class test:
    def __init__(self):
        self.running = True

    def foo(self):
        while(self.running):
            print 'Hello\n'
            sleep(2)

    def getUserInput(self):
        x = ''
        while(x != 'e'):
            x = raw_input('Enter value: ')
        self.running = False

    def go(self):
        th1 = threading.Thread(target=self.foo)
        th2 = threading.Thread(target=self.getUserInput)
        th1.start()
        th2.start()


t = test()
t.go()

My code prints out the first hello and asks for input but nothing after that. What am I doing wrong? Thanks for your help in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T06:44:51+00:00Added an answer on June 11, 2026 at 6:44 am

    Update: The opener was running his code on Windows in IDLE. Regarding I/O it behaves differently than a shell or the Windows command line. His code works on the Windows command line.

    In principle, your code works for me. I am running Python 2.6.5.

    Several comments here:

    1) In your case it would be fine to only have two threads: the main thread and another one. However, it will also work with three. It’s just that your main thread does nothing else than waiting for the other threads to finish.

    2) You should to explicitly join() all threads you spawn. You do this in the main thread before terminating it. Keep record of the threads you spawn (e.g. in a list threads) and then join them at the end of your program (e.g. for t in threads: t.join()).

    3) You share the variable self.running between threads. It is fine in this case, as one thread only reads it and another one only writes it. In general, you need to be very careful with shared variables and acquire a lock before changing it.

    4) You should catch the KeyboardInterrupt exception in the main thread and find a way to communicate to your other threads to terminate 🙂

    5) Use lowercase method names, so instead of getUserInput call it get_user_input. Use uppercase class names and inherit from object: class Test(object):

    This is a running example:

    import threading
    from time import sleep
    
    
    def main():
        t = Test()
        t.go()
        try:
            join_threads(t.threads)
        except KeyboardInterrupt:
            print "\nKeyboardInterrupt catched."
            print "Terminate main thread."
            print "If only daemonic threads are left, terminate whole program."
    
    
    class Test(object):
        def __init__(self):
            self.running = True
            self.threads = []
    
        def foo(self):
            while(self.running):
                print '\nHello\n'
                sleep(2)
    
        def get_user_input(self):
            while True:
                x = raw_input("Enter 'e' for exit: ")
                if x.lower() == 'e':
                   self.running = False
                   break
    
        def go(self):
            t1 = threading.Thread(target=self.foo)
            t2 = threading.Thread(target=self.get_user_input)
            # Make threads daemonic, i.e. terminate them when main thread
            # terminates. From: http://stackoverflow.com/a/3788243/145400
            t1.daemon = True
            t2.daemon = True
            t1.start()
            t2.start()
            self.threads.append(t1)
            self.threads.append(t2)
    
    
    def join_threads(threads):
        """
        Join threads in interruptable fashion.
        From http://stackoverflow.com/a/9790882/145400
        """
        for t in threads:
            while t.isAlive():
                t.join(5)
    
    
    if __name__ == "__main__":
        main()
    

    When typing e or E, the program ends after a short delay (as intended by you). When pressing ctrl+c, it immediately terminates. Making a program that uses threading responsive to exceptions is a bit trickier than expected. I have included important references in the source above.

    This is how it looks like during runtime:

    $ python supertest.py
    
    Hello
    
    Enter 'e' for exit: 
    Hello
    
    
    Hello
    
    
    Hello
    
    e
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some third-party Javascript that has statements like this: FOO = function() {
I have a function foo that accepts a Boolean function def foo( f:(_)=>Boolean )
Say I have a function foo that I want to call n times. In
I have a function that accepts a variable number of parameters: foo (Class... types);
I have a constexpr function that looks something like this: constexpr int foo(int bar)
Let's say that I have an Erlang function, with spec. -spec foo(integer(), string()) ->
void foo() { } That's what I get. I only recently have ventured into
I have a function template that must be allow only certain types. I've seen
I have a structure that only one function must access. The function converts tokens
I have function that receives an array of pointers like so: void foo(int *ptrs[],

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.