我正在尝试在线程中运行线程。当我执行时monitor.startMonitoring(),它卡在该函数中,特别是on threading.Thread(target=self._startMonitoring(), daemon=True).start()。
我该怎么办才能转到代码的最后一行(打印语句)?
谢谢
host = '127.0.0.1'
port = 9000
def _receiveHealthMsg(clientsock, clientaddr):
try:
while True:
print(f'{clientaddr} : {clientsock.recv(1024).decode("utf-8")}: {datetime.datetime.now().isoformat()}')
except ConnectionResetError:
print("Connection has been severed")
class Monitor:
def __init__(self, host, port):
self._sock = socket.socket()
self._sock.bind((host, port))
self._sock.listen(5)
self._threads = []
self._clients = []
def startMonitoring(self):
threading.Thread(target=self._startMonitoring(), daemon=True).start()
def _startMonitoring(self):
while True:
c, addr = self._sock.accept()
thread = threading.Thread(target=_receiveHealthMsg, args=(c, addr))
thread.start()
self._threads.append(thread)
self._clients.append((c, addr))
monitor = Monitor(host, port)
monitor.startMonitoring()
print('do some stuff here while threads are running')
您调用 _start_monitoring,没有将它作为要运行的函数传递。将行更改为:
threading.Thread(target=self._startMonitoring, daemon=True).start() # ^ parentheses removed
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。