0
from threading import *
import time
def handleInput():
    time.sleep(2)
    print "\rhaha\n>",
if __name__ == "__main__":
    worker = Thread(target=handleInput)
    worker.daemon = True
    worker.start()
    clientInput = raw_input("\r>")

">" is just a receiving sign, but when I print a message from another thread, I want to print the ">" on the next line.
The expected output:

haha
> cursor should be in this line and continue to receive the input

The code I show is not working, any help appreciated.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
Sihan Wang
  • 197
  • 1
  • 2
  • 13
  • It's not clear! r u trying to receive text from two threads. eg: when you enter a text then press enter, you switch to using the other thread ? – user3378649 Feb 28 '15 at 21:38
  • no, for this case, it is not necessary to consider the input and enter, I only want to find a way to print message from other thread and then cursor and ">" on the next line of the message("haha" in this case) – Sihan Wang Feb 28 '15 at 21:41

1 Answers1

0

, I only want to find a way to print message from other thread and then cursor and ">" on the next line of the message

You need two threads, not just one, to illustrate the idea that you are trying to work on.

Let's consider this quick example. We have to create two threads, in this case we need to use a [Queue][1] to pass data between threads. This answer can help explain why Queues should be preferred in a threaded environment.

In our example, you are going to update the queue in the first thread (put in the queue), and printing the message that you saved in the other thread (get it from the queue). If the queue is empty, we don't have to print any value.

This example can be a good starting point.

from threading import *
import time
import Queue # Queue in python2.x


def updateInput(messages):
    text= "haha" #"This is just a test"
    messages.put(text)
    #anothertext= "haha" #"This is just a test"
    #messages.put(anothertext)


def handleInput(messages):
    try:
        while True: # Optional, you can loop to, continuously, print the data in the queue
            if not messages.empty():
                text= messages.get()
                print text

            clientInput = raw_input("\r>")
    except KeyboardInterrupt:
        print "Done reading! Interrupted!"

if __name__ == "__main__":
    messages = Queue.Queue()
    writer = Thread(target=updateInput, args=(messages,))
    worker = Thread(target=handleInput, args=(messages,))

    writer.start()
    worker.start()

    writer.join()
    worker.join()

This example should be a starting point, if you have any other question, let us know.

Community
  • 1
  • 1
Taha
  • 1
  • yeah that is a good idea, but in your case, whe I stuck in to the "clientInput = raw_input("\r>")" , I will never be able to print message from other thread. That is not what I want. – Sihan Wang Feb 28 '15 at 22:42
  • You are printing from the other thread print text, where text= messages.get(). You are making a channel to pass data through thread using this queue. – Taha Feb 28 '15 at 22:45