Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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
如何在main仍在python中运行时使用线程来实时获取用户输入_Python_Multithreading - Fatal编程技术网

如何在main仍在python中运行时使用线程来实时获取用户输入

如何在main仍在python中运行时使用线程来实时获取用户输入,python,multithreading,Python,Multithreading,在WHILE循环中,我想运行两个函数,一个是基本函数,每次都会运行,另一个是用户输入函数,当用户输入“解除防护”时,程序可以运行用户输入函数。 这两个函数需要在WHILE循环中运行,因此可以一直运行 如何编写函数来完成此任务 因为它是实时的,所以我不能添加时间。在线程中睡眠 谢谢 import threading class BackInput(threading.Thread): def __init__(self): super(BackInput, self)._

在WHILE循环中,我想运行两个函数,一个是基本函数,每次都会运行,另一个是用户输入函数,当用户输入“解除防护”时,程序可以运行用户输入函数。 这两个函数需要在WHILE循环中运行,因此可以一直运行

如何编写函数来完成此任务

因为它是实时的,所以我不能添加时间。在线程中睡眠

谢谢

import threading

class BackInput(threading.Thread):
    def __init__(self):
        super(BackInput, self).__init__()


    def run(self):
        self.input = raw_input()

while True:
    threading1 = BackInput()
    threading1.start()
    threading1.join()
    if threading1.input == 'disarm':
        print 'Disarm'
        break
    print 'Arm'

在这段代码中,程序应该每秒打印一次Arm,当我键入disarm时,它可以打印disarm并将其破坏。

您确实需要更具体一些。为什么这些需要在线程中?你应该向我们展示你的尝试,或者更详细地描述你试图实现的目标

在当前设置中,您将线程放入循环中,因此它不能独立于每个用户输入运行

编辑:这里有一些经过清理的代码作为示例,基于您的帖子编辑和评论

import threading
import time
import sys

def background():
        while True:
            time.sleep(3)
            print 'disarm me by typing disarm'


def other_function():
    print 'You disarmed me! Dying now.'

# now threading1 runs regardless of user input
threading1 = threading.Thread(target=background)
threading1.daemon = True
threading1.start()

while True:
    if raw_input() == 'disarm':
        other_function()
        sys.exit()
    else:
        print 'not disarmed'

嗨,谢谢你的建议。我想在while循环中运行两个函数,一个是我的基本函数,它将一直运行,另一个是输入函数,当用户输入解除防护时,程序将运行输入函数,否则程序仍运行基本函数。如何使用python实现这一点?谢谢:)你把它放在一个循环中,每次循环运行时它都会运行,在你的例子中,你启动一个线程,重新加入它,然后请求用户输入。在当前示例中,您没有“一直”运行任何东西。如果您希望一个函数在程序运行的整个过程中都运行,并且能够接受用户输入来运行另一个函数,请像我在第二个示例中所做的那样。它将立即启动另一个线程并继续运行,在本例中,它甚至会在任何用户输入上运行,直到他们键入指定的“解除”并运行另一个函数()然后关闭程序。感谢您的患者建议。我已经测试了你的第二个,并在BackInput中添加了print'arm',似乎只打印了一次arm,它是否一直在运行?这是因为你只打印了一次。根据您的帖子编辑和评论查看我的编辑。使用线程构造函数更清晰,我在这里向您展示了while循环和time.sleep(),它可以与任何用户输入同时运行。这是否回答了您的问题?