2

I wrote a little IRC client using sockets. And no I don't want to use a libary like twisted. ;)I am printing messages to stdout and that works all good. Is there a good way to print new messages but also listen to user input with something like threading? Here is a little snippet:

from threading import Thread
import time

def print_stuff():
    while True:
        print "New PRIVMSG!"
        time.sleep(2)

t = Thread(target=print_stuff)
t.start()

while True:
    raw_input(">>")

This produces not quite the output that I need... Output looks something like this:

>>New PRIVMSG!
test test tesNew PRIVMSG!

Is there a way to make this work in cmd or bash?

Thank you in advance!

Edit: Output should look like this.

New PRIVMSG! 
New PRIVMSG! 
>> hello test
New PRIVMSG! 
3
  • what do you want as output? Commented Jul 20, 2015 at 16:40
  • You might want to look into manipulating the cursor in the terminal. This way you can simulate a GUI by keeping the input in one place and the incoming messages in another. Look into pypi.python.org/pypi/blessings or docs.python.org/2/library/curses.html Commented Jul 20, 2015 at 16:41
  • nivix zixer. That looks like it could help me! I'll have a look. Thank you! Commented Jul 20, 2015 at 16:43

1 Answer 1

2

You could create a console lock. In threading there are multiple convenient Lock objects; I'd create one such as:

consoleLock = threading.Lock()

then whenever you want to print to console:

consoleLock.acquire()
print 'what to print'
consoleLock.release()

This will make printing to console thread safe. As for listening to user input at the same time, how exactly do you want to accomplish this? Anything printed during input will still end up in the middle of the input area as there's only one console to print to (and read input is printed to console).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.