如何在一个输出影响其他动作的情况下如何使用两个函数

这是我的代码:

import time
import keyboard #pip install keyboard - could use pynput listener instead.
from threading import Thread

hshtag = int(0)
done = False
fire = False


def StopStart(fire):
    while not done:
        global fire
        if keyboard.is_pressed('#'):
            hshtag = hshtag + 1
            if hshtag % 2 ==0:
                fire = False
            else:
                fire = True
        return fire


def NormalFire():
    while not done:
        global fire
        if fire == True:
            #do x        
        else:
            pass

t1 = Thread(target = StopStart)
t2 = Thread(target = NormalFire(fire))
t1.start()
t2.start()

问题是函数StopStart(应)影响Normalfire的功能,但由于该函数仅在开始运行时才接受fire的值(因此不起作用)。我想要的是更改normalfire用stopstart执行的功能。如果您想知道为什么要使用线程化,因为“ #do x”实际上需要一段时间才能工作,所以如果我在错误的时间单击哈希值,它就不会停止,因此作为一个连续脚本。也许我可以改为使用类,但不能很好地使用类,因此如果有人可以提供帮助或修复上面的代码,将非常感谢。

尝试解释顶层代码出了什么问题的新尝试-好的,因此这两个函数应该同时运行(它们确实在运行)-因此,那里没有问题。但是当函数StopStart将布尔火更改为true / false时,我希望它使我的NormalFire函数更改其所做的事情-当我还没有单击哈希时什么也没有,如果我单击一次哈希则没有什么,但是如果我单击哈希在其运行时将完成其运行,然后不执行任何操作,等待再次单击哈希。

对不起,我不清楚我的问题是否将这段代码简化为我的核心问题。

##imports
import time
import keyboard #pip install keyboard - could use pynput listener instead.
from threading import Thread

##variable assigning
hshtag = int(0)
done = False
fire = False


def x():
    while not done:
        fire = True
        return fire

def y(fire):
    while not done:
        if fire:
            print('ok')                   
        else:
            pass

t1 = Thread(target = x)
t2 = Thread(target = y(fire))
t1.start()
t2.start()

当前,即使我在函数x中设置了“ fire = true”并返回该代码,上述代码也不会输出任何内容,我将如何编辑此代码,以便当布尔fire更改为true时,函数y开始可以打印吗?>

像Nair一样建议编辑也不会返回任何内容,并且15秒钟后程序将停止运行编辑后的代码:

##imports
import time
import keyboard #pip install keyboard - could use pynput listener instead.
from threading import Thread

##variable assigning
hshtag = int(0)
done = False
fire = False


def StopStart():
    while not done:
        fire = True
        return fire

def NormalFire():
    while not done:
        if fire:
            print('ok')                   
        else:
            pass

t1 = Thread(target = StopStart)
t2 = Thread(target = NormalFire)
t1.start()
t2.start()
zxpdm4 回答:如何在一个输出影响其他动作的情况下如何使用两个函数

我无法发表评论,因此我深表歉意。我在理解您上面的问题时遇到了麻烦,但是我重新编写了您的代码-修复/添加您需要的所有内容并回复我!

Can you just flush the error? You can. I wouldn't. What else is there?
,

不确定这正是您所要的。我可能会在任何一个线程之外监听键盘事件。相反,只要将键盘事件绑定到设置了threading.Event对象的回调即可。很抱歉出现这个怪异的病态示例:

from pynput import keyboard
from threading import Thread,Event

plate_dropped = Event()

def on_press(key):
    if key is keyboard.Key.enter:
        plate_dropped.set()

listener = keyboard.Listener(on_press=on_press)

def poll_plate_status():
    from time import sleep
    from random import choice
    messages = [
        "It sure is tempting,eh?","Are you gonna do it?"
    ]
    print("It'd be a shame if someone would drop this plate and scare grandpa!")
    while not plate_dropped.is_set():
        print(choice(messages))  
        sleep(0.5)
    print("The plate has been dropped!")

def poll_grandpa_status():
    from time import sleep
    from random import choice
    messages = [
        "*zzzZZZzzz*","*Snoooreee*"
    ]
    print("Grandpa is sound asleep.")
    while not plate_dropped.is_set():
        print(choice(messages))
        sleep(0.5)
    print("HUH!?")


plate_thread = Thread(target=poll_plate_status,daemon=True)
grandpa_thread = Thread(target=poll_grandpa_status,daemon=True)

plate_thread.start()

grandpa_thread.start()

listener.start()
,
##imports
import time
import keyboard #pip install keyboard - could use pynput listener instead.
from threading import Thread

##variable assigning
hshtag = int(0)
done = False
fire = False


def StopStart(self,interval=1):
    hshtag = 0
    self.interval = interval
    while not done:
        if keyboard.is_pressed('#'):
            hshtag = hshtag + 1
            if hshtag % 2 ==0:
                fire = False
            else:
                fire = True

def NormalFire():
    while not done:
        print('NormalFire Runs')
        time.sleep(1)
        if fire:
            print('*fires*')                   
        else:
            print('*does nothing*')

#t1 = Thread(target = StopStart,daemon=True)
t2 = Thread(target = NormalFire,daemon=True)
#t1.start()
t2.start()
while not done:
    #time.sleep()
    if keyboard.is_pressed('#'):
        hshtag = hshtag + 1
        time.sleep(0.1)
        if hshtag % 2 ==0:
            fire = False
            print(fire)
        else:
            fire = True
            print(fire)

我意识到我的问题是我对线程的开发(这是我的新手),可以实现我想要的所有帮助。

本文链接:https://www.f2er.com/3134857.html

大家都在问