gpt4 book ai didi

python - 如何线程化两个函数,其中一个输出影响其他操作

转载 作者:太空宇宙 更新时间:2023-11-03 20:09:50 25 4
gpt4 key购买 nike

这是我的代码:

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 值(因此它不起作用)。我想要的是用函数 stopstart 来改变函数 normalfire 的功能。如果您想知道为什么我要使用线程化它,因为“#do x”实际上需要一段时间才能工作,因此作为一个连续脚本,如果我在错误的时间单击哈希,它就不会停止。也许我可以用类来做到这一点,但我不擅长类,所以如果有人可以帮助解决这个问题或修复上面的代码,那就太好了。

新尝试解释顶部代码的问题 - 好吧,所以两个函数应该同时运行(它们确实如此) - 所以没有问题。但是当函数 StopStart 将 bool 火更改为 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”并返回它,上述代码也不会输出任何内容,我将如何编辑此代码,以便当 bool 值 fire 更改为 true 时,函数 y 开始打印正常? p>

像 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()

最佳答案

我无法发表评论,所以我提前道歉。我无法理解您上面的问题,但我重新编写了您的代码 - 修复/添加您需要的任何内容,然后回复我!

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():
while not done:
# global fire - You're setting StopStart up for a param that needs passed, that also is named another variable
# So it will just over write it (Also, no arg is passed for StopStart(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 - Don't need to global it, you would've had to global done if that was the case
if fire: # don't need == true, just need if fire (if true)
print("x")

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

关于python - 如何线程化两个函数,其中一个输出影响其他操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58774363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com