当控制台输入True循环时停止



我正在运行python 2.7.12

现在,我可以告诉它开始,它将开始循环…没有问题。但我无法阻止它。它被困在循环中,所以我无法输入命令让它停止。

怎么才能跳出循环,让我可以下命令?

def dothis():
    while True
        # Loop this infinitely until I tell it stop

while True:
    command = raw_input("Enter command:")
    if command = "start":
        dothis()
    if command = "stop":
        #Stop looping dothis()

这样使用线程:

import threading
import time
class DoThis(threading.Thread):
    def __init__( self ):
        threading.Thread.__init__( self )
        self.stop = False
    # run is where the dothis code will be
    def run( self ):
        while not self.stop:
            # Loop this infinitely until I tell it stop
            print( 'working...' )
            time.sleep( 1 )
a = None
while True:
    command = raw_input("Enter command:")
    if command == "start":
        a = DoThis()
        a.start()
    if command == "stop":
        a.stop = True
        a.join()
        a = None

最新更新