gpt4 book ai didi

python - 运行时错误 : threads can only be started once

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

我正在制作一个简单的 Python 游戏。我试图修复以下错误:

RuntimeError: threads can only be started once

我已经尝试了 .cancel() 计时器,但它似乎没有用,我做了一个 if 语句来查看计时器是否 .is_alive 在执行之前。控制台在 ball_char = play_timer.start() 处抛出错误。

def playball(state):
batbox = [["@", "@", "@", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "@", "@", "@"]]
play = "playing"
play_timer = Timer(1.0, pitch(batbox))
end_timer = Timer(6.0, pitch_end(play))
play_timer.cancel()
end_timer.cancel()
pstate = "idle"
inning = 1
outs = 0
pscore =0
cscore = 0
strikes = 0
ball_row = 0
ball_col = 0
ball_char = "."
while play == "playing":
batbox = [["@", "@", "@", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "#", "#", "#", "@"], ["@", "@", "@", "@"]]
os.system('cls') # on windows
os.system('clear') # on linux / os x
# Playing the game
print "Press enter to start / hit the pitch"
print_grid(batbox)
input = raw_input("")
pstate = "hitting"
# Hitting
if pstate == "hitting":
ball_row = random_row(batbox)
ball_col = random_col(batbox)
end_timer.start()
while pstate == "hitting":
batbox[ball_row][ball_col] = ball_char
if play_timer.is_alive():
play_timer.cancel()
else:
ball_char = play_timer.start()
else:
play_timer.cancel()
end_timer.cancel()
state = "mainmenue"
return state

最佳答案

documentation for Threading.Thread (其中 Timer 是一个子类)指出:

start() Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

This method will raise a RuntimeError if called more than once on the same thread object.

即使你取消了一个线程(或定时器),你仍然不能再次调用start()。您需要创建一个新的线程/计时器对象,否则会出错。

关于python - 运行时错误 : threads can only be started once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46992496/

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