python - raw_input和thread

时间:2014-09-02 20:50:53

标签: python linux

我有一个线程验证我的数据库和raw_input的结果以输入更多数据。问题是线程打印在raw_input上。这样的事情。

input> DATAOFTHREAD

但我想打印出类似的结果

DATAOFTHREAD
input>

...

def handler(de_user,idChat):
    db = MySQLdb.connect('localhost','user','passwd','database')
    cursor = db.cursor()
    oldata = 0
    write = sys.stdout.write
    while True:
        db.commit()
        cursor.execute("SELECT id FROM mensajes WHERE De_user='%s' AND idChat='%s' ORDER BY id DESC LIMIT 0,1"%(de_user,idChat))
        traer_id = cursor.fetchall()
        if traer_id:
            for j in traer_id:
                data = j[0]
        if oldata != 0:
            if oldata != data:
                cursor.execute("SELECT Mensaje FROM mensajes WHERE De_user='%s' AND id>'%d'"%(de_user,oldata))
                traer_mensajes = cursor.fetchall()
                if traer_mensajes:
                    for i in traer_mensajes:
                        Mensaje = i[0]
                        Mensaje = base64.decodestring(Mensaje)
                        write(de_user+' > '+Mensaje)
                        write('\n')
                    oldata = data
        else:
            oldata = data

t = Thread(target=handler, args=(de_user, idChat))
t.daemon = True
t.start()
while True:
    contestar = raw_input('> ')
    print "\033[A                                        \033[A"
    #Validations

解决: Reading input from raw_input() without having the prompt overwritten by other threads in Python

感谢那些帮助过我的人

0 个答案:

没有答案