0

So I currently have a thread that is outputting, and I need a way to handle input while still allowing output from the thread in the meantime.

Currently I have something like this

def thread_func():
    while True:
        print("Information")

threading.Thread(target=thread_func).start()

while True:
    command = raw_input("Enter a command:")
    #dostuff with command

My issue now is that my thread isn't printing anything. Or if it is it isn't showing up

Edit:

Found a solution here Exiting while loop by pressing enter without blocking. How can I improve this method?

Community
  • 1
  • 1
Nerdsie
  • 116
  • 1
  • 3
  • 11
  • I cannot duplicate this behavior. In fact, when I run this same code in Python2 it prints `"Information"` so quickly I'm unable to enter the command. Can you be more specific in the behavior you're seeing? – Adam Smith Mar 30 '17 at 19:20
  • You're right, that does run. I was trying to simplify the code because it's kinda complicated and very specific to what I'm working on. Basically in the thread I'm looping over a stream and I guess for some reason that's causing the issue. I ended up fixing the problem by looking at the answer supplied here http://stackoverflow.com/questions/22391134/exiting-while-loop-by-pressing-enter-without-blocking-how-can-i-improve-this-me/22391379#22391379 – Nerdsie Mar 30 '17 at 20:55
  • http://stackoverflow.com/questions/22391134/exiting-while-loop-by-pressing-enter-without-blocking-how-can-i-improve-this-me/22391379#22391379 The answer to this is what I used to fix my program – Nerdsie Mar 30 '17 at 20:55
  • Awesome I'm glad it's working! I'm closing this question as a duplicate of the linked question. – Adam Smith Mar 30 '17 at 20:57

1 Answers1

-2

You want to use a Queue. New input will go into the back of the queue, and work will be started from the front.

Efron Licht
  • 488
  • 3
  • 13