PythonSleekXMPPライブラリを使用してFarkleJabberゲームボットを作成しました。マルチプレイヤーモードでは、ユーザーは順番にユーザーと対戦します。たとえば、対戦相手が1分以内に応答しなかった場合に勝つように、タイムアウト期間を作成しようとしています。
これが私が試したことです:
import sleekxmpp
...
time_received={}
class FarkleBot(sleekxmpp.ClientXMPP):
...
def timeout(self, msg, opp):
while True:
if time.time() - time_received[opp] >= 60:
print "Timeout!"
#stuff
break
def messages(self, msg):
global time_received
time_received[user] = time.time()
...
if msg['body'].split()[0] == 'duel':
opp=msg['body'].split()[1] #the opponent
... #do stuff and send "Let's duel!" to the opponent.
checktime=threading.Thread(target=self.timeout(self, msg, opp))
checktime.start()
上記のコードの問題は、1分が経過するまでクラス全体がフリーズすることです。どうすればそれを回避できますか?timeout
機能をクラスの外に出してみましたが、何も変わりませんでした。