How do I read user input in python thread?

Posted by Sid H on Stack Overflow See other posts from Stack Overflow or by Sid H
Published on 2010-05-03T10:12:44Z Indexed on 2010/05/03 10:18 UTC
Read the original article Hit count: 246

Filed under:
|

I'm trying to read from a thread in python as follows

import threading, time, random

var = True

class MyThread(threading.Thread):
    def set_name(self, name):
        self.name = name

    def run(self):
        global var
        while var == True:
            print "In mythread " + self.name
            time.sleep(random.randint(2,5))


class MyReader(threading.Thread):
    def run(self):
        global var
        while var == True:
            input = raw_input("Quit?")
            if input == "q":
                var = False



t1 = MyThread()
t1.set_name("One")

t2 = MyReader()

t1.start()
t2.start()

However, if I enter 'q', I see the following error.

In mythread One
Quit?q
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.6/threading.py", line 522, in __bootstrap_inner
    self.run()
  File "test.py", line 20, in run
    input = raw_input("Quit?")
EOFError

In mythread One
In mythread One

How does on get user input from a thread?

© Stack Overflow or respective owner

Related posts about python

Related posts about threading