Non-Blocking raw_input() in python

后端 未结 4 1993
感情败类
感情败类 2020-12-11 05:11

After digging around in SO for a while, I still haven\'t found a good answer to what I would hope is a fairly common need. Basically I need a main thread to do \"stuff\" unt

4条回答
  •  半阙折子戏
    2020-12-11 05:58

    I found the accepted answer didn't work for me - it would still block at raw_input even in a separate thread. However, when I switched the threads around it worked right away.

    import threading 
    
    def mainWork():
      while 1:
        #whatever you wanted to do until an input is received
    
    myThread = threading.Thread(target=mainWork)
    myThread.start()
    
    while 1:
      input = raw_input()
      #do stuff with input when it is received
    

提交回复
热议问题