gpt4 book ai didi

Python线程设计

转载 作者:行者123 更新时间:2023-11-28 17:41:30 25 4
gpt4 key购买 nike

我正在尝试编写一个小游戏来练习我的 Python 线程技能。游戏本身涉及定时炸弹和拥有定时炸弹的城市。

这是我的代码:

class City(threading.Thread):

def __init__(self, name):
super().__init__()
self.name = name
self.bombs = None
self.activeBomb = None
self.bombID = 0
self.exploded = False

def addBomb(self, name, time, puzzle, answer, hidden=False):
self.bombs.append(Bomb(name, self.bombID, time, puzzle, answer, hidden))
self.activeBomb.append(self.bombID)
self.bombID += 1

def run(self):
for b in self.bombs:
b.start()
while True:
# listen to the bombs in the self.bombs # The part that I dont know how

# if one explodes
# print(self.name + ' has been destroyed')
# break
# if one is disarmed
# remove the bombID from the activeBomb
# if all bombs are disarmed (no activeBomb left)
# print('The city of ' + self.name + ' has been cleansed')
# break


class Bomb(threading.Thread):

def __init__(self, name, bombID, time, puzzle, answer, hidden=False):
super(Bomb, self).__init__()
self.name = name
self.bombID = bombID
self._timer = time
self._MAXTIME = time
self._disarmed = False
self._puzzle = puzzle
self._answer = answer
self._denoted = False
self._hidden = hidden

def run(self):
# A bomb goes off!!
if not self._hidden:
print('You have ' + str(self._MAXTIME)
+ ' seconds to solve the puzzle!')
print(self._puzzle)
while True:
if self._denoted:
print('BOOM')
// Communicate to city that bomb is denoted
break
elif not self._disarmed:
if self._timer == 0:
self._denoted = True
else:
self._timer -= 1
sleep(1)
else:
print('You have successfully disarmed bomb ' + str(self.name))
// Communicate to city that this bomb is disarmed
break

def answerPuzzle(self, ans):
print('Is answer ' + str(ans) + ' ?')
if ans == self._answer:
self._disarmed = True
else:
self._denotaed = True

def __eq__(self, bomb):
return self.bombID == bomb.bombID

def __hash__(self):
return id(self)

我目前不知道城市类有效跟踪的好方法是什么炸弹状态。

我的第一个想法是使用 for 循环让 City 检查城市,但我发现它太愚蠢且效率低下

问题来了:

实现炸弹和城市的最有效方式是什么,以便城市立即知道炸弹的状态变化,而无需每秒检查一次?

PS:我并不是要用这个程序来引爆真正的炸弹,所以放轻松 :D

最佳答案

一个使用队列的好例子。这是所谓的生产者 - 消费者模式的示例。

工作线程将永远运行,直到您的主程序完成(这就是守护程序部分和“while True”的用途)。他们将认真监控工作包的 in_queue。他们将处理包裹,直到没有剩下为止。因此,当加入 in_queue 时,您的工作线程的工作就完成了。这里的out_queue是一个可选的下游处理步骤。因此,您可以将工作线程中的各个部分组装成摘要形式。当它们在函数中时很有用。

如果您需要一些输出,例如每个工作线程将结果打印到屏幕或写入一个文件,请不要忘记使用信号量!否则,您的输出会相互碰撞。

祝你好运!

from threading import Thread
import Queue

in_queue = Queue.Queue()
out_queue = Queue.Queue()

def work():
while True:
try:
sonId = in_queue.get()
###do your things here
result = sonID + 1

###you can even put your thread results again in another queue here
out_queue.put(result) ###optional


except:
pass
finally:
in_queue.task_done()

for i in range(20):

t = Thread(target=work)
t.daemon = True
t.start()


for son in range(10):
in_queue.put(son)


in_queue.join()

while not out_queue.empty():

result = out_queue.get()

###do something with your result here

out_queue.task_done()

out_queue.join()

关于Python线程设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23572426/

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