Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在用户决定是否在原始输入中写入任何内容时,如何执行其他操作?_Python_Multithreading_Raw Input - Fatal编程技术网

Python 在用户决定是否在原始输入中写入任何内容时,如何执行其他操作?

Python 在用户决定是否在原始输入中写入任何内容时,如何执行其他操作?,python,multithreading,raw-input,Python,Multithreading,Raw Input,虽然用户不键入任何内容,但我无法执行其他操作。我该怎么做呢,其他的东西会被执行,但是,如果用户在那个时候键入任何东西,mess会改变它的值?您应该在工作线程中生成其他东西 while True: mess = raw_input('Type: ') //other stuff 这是一个将mess作为全局变量的小例子。请注意,对于工作线程和主线程之间对象的线程安全传递,应该使用队列对象在线程之间传递内容,而不要使用全局对象 作为使用工作线程的替代方法,您可以通过以下方式轮询用户输入在Un

虽然用户不键入任何内容,但我无法执行其他操作。我该怎么做呢,其他的东西会被执行,但是,如果用户在那个时候键入任何东西,mess会改变它的值?

您应该在工作线程中生成其他东西

while True:
  mess = raw_input('Type: ')
  //other stuff

这是一个将
mess
作为全局变量的小例子。请注意,对于工作线程和主线程之间对象的线程安全传递,应该使用
队列
对象在线程之间传递内容,而不要使用全局对象

作为使用工作线程的替代方法,您可以通过以下方式轮询用户输入在Unix上是否可用:


每次用户按Enter键时,
mess
值都会更改。

我看到了你的文章,决定做一些挖掘来帮助你解决问题。我不知道你以前是否看过这篇文章,但我发现这篇文章和用户jhackworth的投票结果似乎解决了你提到的问题。我希望这能帮助你,至少能为你指明正确的方向!可能重复的
import threading
import time
import sys

mess = 'foo'

def other_stuff():
  while True:
    sys.stdout.write('mess == {}\n'.format(mess))
    time.sleep(1)

t = threading.Thread(target=other_stuff)
t.daemon=True
t.start()

while True:
  mess = raw_input('Type: ')
import select
import sys

def raw_input_timeout(prompt=None, timeout=None):
    if prompt is not None:
        sys.stdout.write(prompt)
        sys.stdout.flush()
    ready = select.select([sys.stdin], [],[], timeout)[0]
    if ready:
        # there is something to read
        return sys.stdin.readline().rstrip('\n')

prompt = "First value: "
while True:
    s = raw_input_timeout(prompt, timeout=0)
    if s is not None:
        mess = s # change value
        print(repr(mess))
        prompt = "Type: " # show another prompt
    else:
        prompt = None # hide prompt
    # do other stuff