views:

68

answers:

2

I have a script which when first run creates a new thread which logs certain events. After the thread has been created, I ask for some input on user with the following code:

user_input = raw_input('>> ')

So when the script it run the user receives the '>>' prompt, but when the logger from the created thread starts logging it starts to look like this:

>> Error: Some random error  
Error: Another error

As you can see it messes-up and lost the prompt. What I wanna do is have the log messages appear but still maintain my cursor on the prompt ready for some input. like:

>>  
Error: Some random error  
Error: Another error
>>
+2  A: 

The problem is that your raw_input() is running on a completely different thread, and has no idea that the logger just spewed out some log messages. So there is no way for raw_input() to know that it should re-draw the prompt.

I don't have any simple solution for you. All I can think of is for the logger thread to not print the messages, but append them to some shared list that the raw_input() thread can see, and have that thread print the messages after the user enters input.

Actually, I do have one other idea: you could draw the >> prompt up at the top of the window, and have the logger messages appear lower in the window. If they are spatially separated, it doesn't matter if they are interleaved in time.

steveha
+1  A: 

The obvious (and probably the only correct) solution is to log into a separate file, rather than on standard output. If you still want to observe the logging output in real time, you can use a command such as tail -f on the logging file.

Antoine P.
If using tail -f won't mess the prompt then its good enough. Also, is there a simple tail -f equivalent for python? Or do I have to read the last line manually?
Marconi
This is a nice simple solution. You run the `tail -f` in another command line window. You could even have Python start up a GUI equivalent to `tail -f` as a background process. StackOverflow discussion of the best GUI `tail -f` equivalent: http://stackoverflow.com/questions/133821/the-best-tail-gui
steveha