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