So I have a threaded Python program that takes input from a user and prints data simultaneously. The problem is that when the program is sitting at raw_input(), it won't print anything and will print it all after the user presses enter.
Is there any way to have u开发者_JAVA技巧ser input and print at the same time?
You have two options, basically: threading and asynchronous IO.
You can have one thread fill a Queue with entered data and have the other thread print its contents. Be warned that threading is hard (impossible?) to do right.
Asynchronous IO means you have a main dispatcher that invokes callbacks when data is available (i.e. the user has entered data). There are frameworks that abstract most of this for you, such as asyncore and Twisted.
Most GUI toolkits will also implement an asynchronous dispatching system through their mainloop, ie.. Tkinter, wxWidgets and pygtk. This will also solve the interface problems you have when mixing reading from and writing to the same (terminal) screen.
精彩评论