我遇到了一个很难找到解决方案的问题
我正在运行一个新的python线程,该线程随后被套接字连接(python socketio)阻塞,因为它无限期地等待数据。但我需要在5分钟后关闭此线程。
我试图设置一个计时器,用sys.exit()
关闭线程,但我发现这是关闭计时器线程本身
以下是迄今为止的代码:
class LiveMatches(commands.Cog):
def __init__(self, client):
self.client = client
def connect_to_scorebot(self, id):
feed = MatchFeed(self.client)
feed.main(id) # when this function is called,
# it will block the newly created thread to receive the data with the socket
def create_thread(self, id):
# we create the thread and call launch_match that will connect to
# the scorebot
new_thread = threading.Thread(target=self.connect_to_scorebot, args=(id,))
# start the thread
new_thread.start()
有两种选择:
#1的示例:
输出:
注意:服务器没有打印第二个客户端已断开连接。因为它是一个守护进程线程,所以当主线程和客户端线程都退出时,它被终止,并且没有时间识别超时后断开的客户端。如果您想要更干净的退出行为,请不要使用守护进程线程
相关问题 更多 >
编程相关推荐