如何在提供输入的同时输出到控制台?

时间:2014-04-21 23:40:52

标签: python multithreading console irc

基本上,我想要这个帖子中的内容:Output to console while preserving user input in ruby,但是在Python中。我已经google了很长一段时间,发现了一个ALMOST工作解决方案,只要它阻止了主线程,只要我没有输入任何内容并按下回车键。

我不想发生的一些输出是:

/raw:jtv!jtv@jtv.tmi.twitch.tv PRIVMSG #cobaltstreak :USERCOLOR ullr_son_of_sif #DAA520

我想要的一些示例输入是:

:jtv!jtv@jtv.tmi.twitch.tv PRIVMSG #cobaltstreak :USERCOLOR ullr_son_of_sif #DAA520
/raw
PRIV:jtv!jtv@jtv.tmi.twitch.tv PRIVMSG #cobaltstreak :SPECIALUSER nightbot subscriber
MSG #cobaltstreak :This shouldn't be here, but on the same line with /raw

这意味着,我希望控制台的底线保留输入,同时输出主线程中发生的所有事情而不影响输入。

我目前的代码是:

def console(q, m, lock):
    while 1:
    raw_input()   # After pressing Enter you'll be in "input mode"
    with lock:
        i = raw_input('> ')
        cmd = i.split(' ')[0]
        msg = i.strip(cmd + ' ')
    q.put(cmd)
    m.put(msg)
    if cmd == 'quit':
        break

还有:

cmd = cmd_queue.get()
msg = msg_queue.get()
action = cmd_actions.get(cmd)
if action is not None:
    action(stdout_lock, msg)

注意上面的代码是我的while循环中的第一行。

我在Windows上使用python 2.7.6

0 个答案:

没有答案
相关问题