SocketClientThread 참조 큐. (thanks again Eli!)다중 스레드 소켓 연결/
하나의 스레드가 훌륭하게 작동하지만 동시에 여러 SocketClientThread 객체를 관리하려고합니다.
연결 후, 두 번째 객체에 데이터를 보내려고하면 SocketClientThread.socket에 sendall 속성이 없다는 메시지가 표시되므로 소켓이 꺼져 있다고 생각합니다.
1과 2 모두의 서버가 성공적으로 연결을 수신하지만 오류를 트리거하는 것은 두 번째 서버의 send 명령입니다.
어떻게이 클래스를 여러 객체와 함께 재사용 할 수 있습니까? 여기
출력입니다 :$ python testmodule.py
('sct1: ', 1, None)
('sct1: ', 1, 'tuxy')
('sct2: ', 1, None)
Exception in thread Thread-2:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py", line 522, in __bootstrap_inner
self.run()
File "testmodule.py", line 59, in run
self.handlers[cmd.type](cmd)
File "testmodule.py", line 115, in _handle_SEND
self.socket.sendall(header + cmd.data)
AttributeError: 'NoneType' object has no attribute 'sendall'
그리고 여기 내 코드는 모두 개방하려고 : 들어
if __name__ == "__main__":
sct1 = SocketClientThread()
sct1.start()
sct1.cmd_q.put(ClientCommand(ClientCommand.CONNECT, ('', 50007)))
reply = sct1.reply_q.get(True)
sct1.cmd_q.put(ClientCommand(ClientCommand.SEND, "hellothere from sct1"))
reply = sct1.reply_q.get(True)
print('sct1: ', reply.type, reply.data)
sct1.cmd_q.put(ClientCommand(ClientCommand.RECEIVE, "hellothere from sct1"))
reply = sct1.reply_q.get(True)
print('sct1: ', reply.type, reply.data)
sct2 = SocketClientThread()
sct2.start()
sct2.cmd_q.put(ClientCommand(ClientCommand.CONNECT, ('', 50008)))
reply = sct2.reply_q.get(True)
print('sct2 connect: ', reply.type, reply.data)
sct2.cmd_q.put(ClientCommand(ClientCommand.SEND, "hellothere from sct2"))
reply = sct2.reply_q.get(True)
print('sct2 send: ', reply.type, reply.data)
sct2.cmd_q.put(ClientCommand(ClientCommand.RECEIVE, "hellothere from sct2"))
reply = sct2.reply_q.get(True)
print('sct2: ', reply.type, reply.data)
#close connection 1
sct1.cmd_q.put(ClientCommand(ClientCommand.CLOSE))
reply = sct1.reply_q.get(True)
print('sct1 close: ', reply.type, reply.data)
#close connection 2
sct2.cmd_q.put(ClientCommand(ClientCommand.CLOSE))
reply = sct2.reply_q.get(True)
print('sct2 close: ', reply.type, reply.data)
감사합니다. 내 소켓 및 대기열이 모두 예상대로 작동하고 있음을 확인했습니다. 대기열이 생성 된 방법이 마음에 들었지만 소켓을 죽일 수있는 방법을 이해할 수 없었습니다. 나는 그들이 같은 큐 객체를 공유하고있는 것을 본다. 고맙습니다! – Coder1
@ Coder1 : 그래, 다른 스레드가 첫 번째 소켓을 죽일 요청을 받았고 자신의 소켓을 죽였지 만 보내기/받기 요청을 받았다. –
그냥 호출해서는 안된다. 스레드를 종료하려면 부모 개체에서 mysocketclientthread.join()? 나는 그 두 가지를 시도하고 결합을위한 큐 핸들러를 추가했다. 응용 프로그램이 중단되고 부모 개체가 설정되지 않은 것처럼 보입니다. 어쩌면 다시 좋은 의견이 아니기 때문일 수도 있습니다. – Coder1