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

  • Home
  • SEARCH
  • 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 3680840
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:37:26+00:00 2026-05-19T03:37:26+00:00

I’m trying to let the user input commands at a console using raw_input(), this

  • 0

I’m trying to let the user input commands at a console using raw_input(), this works fine. The problem is I have background threads that occasionally output log-information to the screen and when they do they mess up the input prompt (since the output go wherever the cursor happens to be at the moment).

This is a small Python program that illustrate what i mean.

#!/usr/bin/env python
import threading
import time

def message_loop():
    while True:
        time.sleep(1)
        print "Hello World"

thread = threading.Thread(target = message_loop)
thread.start()

while True:
    input = raw_input("Prompt> ")
    print "You typed", input

This is an example of what it could look like when I run it:

Prompt> Hello World
Hello World
Hello World
Hello World
test
You typed test
Prompt> Hello World
Hello World
Hello World
hellHello World
o
You typed hello
Prompt> Hello World
Hello World
Hello World
Hello World

What I want is for the prompt to move along with the output from the thread. Like so:

Hello World
Hello World
Prompt> test
You typed test
Hello World
Hello World
Hello World
Hello World
Hello World
Prompt> hello
You typed hello
Hello World
Hello World
Hello World
Hello World
Prompt> 

Any ideas on how to achieve this without resorting to ugly hacks? 🙂

  • 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-05-19T03:37:27+00:00Added an answer on May 19, 2026 at 3:37 am

    I recently encountered this problem, and would like to leave this solution here for future reference.
    These solutions clear the pending raw_input (readline) text from the terminal, print the new text, then reprint to the terminal what was in the raw_input buffer.

    This first program is pretty simple, but only works correctly when there is only 1 line of text waiting for raw_input:

    #!/usr/bin/python
    
    import time,readline,thread,sys
    
    def noisy_thread():
        while True:
            time.sleep(3)
            sys.stdout.write('\r'+' '*(len(readline.get_line_buffer())+2)+'\r')
            print 'Interrupting text!'
            sys.stdout.write('> ' + readline.get_line_buffer())
            sys.stdout.flush()
    
    thread.start_new_thread(noisy_thread, ())
    while True:
        s = raw_input('> ')
    

    Output:

    $ ./threads_input.py
    Interrupting text!
    Interrupting text!
    Interrupting text!
    > WELL, PRINCE, Genoa and Lucca are now no more than private estates of the Bo
    Interrupting text!
    > WELL, PRINCE, Genoa and Lucca are now no more than private estates of the Bo
    naparte family. No, I warn you, that if you do not tell me we are at war,
    

    The second correctly handles 2 or more buffered lines, but has more (standard) module dependencies and requires a wee bit of terminal hackery:

    #!/usr/bin/python
    
    import time,readline,thread
    import sys,struct,fcntl,termios
    
    def blank_current_readline():
        # Next line said to be reasonably portable for various Unixes
        (rows,cols) = struct.unpack('hh', fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ,'1234'))
    
        text_len = len(readline.get_line_buffer())+2
    
        # ANSI escape sequences (All VT100 except ESC[0G)
        sys.stdout.write('\x1b[2K')                         # Clear current line
        sys.stdout.write('\x1b[1A\x1b[2K'*(text_len/cols))  # Move cursor up and clear line
        sys.stdout.write('\x1b[0G')                         # Move to start of line
    
    
    def noisy_thread():
        while True:
            time.sleep(3)
            blank_current_readline()
            print 'Interrupting text!'
            sys.stdout.write('> ' + readline.get_line_buffer())
            sys.stdout.flush()          # Needed or text doesn't show until a key is pressed
    
    
    if __name__ == '__main__':
        thread.start_new_thread(noisy_thread, ())
        while True:
            s = raw_input('> ')
    

    Output. Previous readline lines cleared properly:

    $ ./threads_input2.py
    Interrupting text!
    Interrupting text!
    Interrupting text!
    Interrupting text!
    > WELL, PRINCE, Genoa and Lucca are now no more than private estates of the Bo
    naparte family. No, I warn you, that if you do not tell me we are at war,
    

    Useful sources:

    How to get Linux console window width in Python

    apt like column output – python library
    (This code sample shows how to get terminal width for either Unix or Windows)

    http://en.wikipedia.org/wiki/ANSI_escape_code

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.