擅长:python、mysql、java
<p>下面是一个多线程示例。此代码将与python解释器一起工作,但与IDLE的python Shell不一起工作,因为<code>raw_input</code>函数的处理方式不同。你知道吗</p>
<pre><code>from threading import Thread
from time import sleep
class A(Thread):
def __init__(self):
Thread.__init__(self)
self.value = 1
self.stop_flag = False
def run(self):
while not self.stop_flag:
sleep(1)
print(self.value)
def set_value(self, value):
self.value = value
def stop(self):
self.stop_flag = True
if __name__ == '__main__':
a = A()
a.start()
try:
while 1:
r = raw_input()
a.set_value(int(r))
except:
a.stop()
</code></pre>