SocketClientThread參考:http://eli.thegreenplace.net/2011/05/18/code-sample-socket-client-thread-in-python/多個螺紋套接字連接/隊列
以利提供瞭如何管理一個插座客戶線程內,並傳送到與該隊列模塊的主要範圍這個通用例子。 (再次感謝Eli!)
一個線程工作的很好,但我試圖做的是同時管理多個SocketClientThread對象。
連接後,當我嘗試發送數據到第二個對象時,它告訴我SocketClientThread.socket沒有sendall屬性,所以我認爲套接字被取消。
1和2的服務器都成功接收連接,但它是觸發錯誤的第二個服務器的發送命令。
如何重新使用這個類與多個對象?
下面是輸出:
$ 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