【问题标题】:Python Handling Socketserver request in the ThreadPython在线程中处理Socketserver请求
【发布时间】:2011-12-16 13:11:08
【问题描述】:

在下一个示例中,我在线程中处理了 Socket 服务器。在句柄函数中,我们创建线程并传递 request 和 client_address。 当我们在线程的运行方法中处理请求时,它只能接收数据,但是当我尝试发送时会引发异常([Errno 9] 错误的文件描述符) 怎么了?

import SocketServer
import threading


class MyServerThread(threading.Thread):

    def __init__(self, channel, details):
        self.channel = channel
        self.details = details
        threading.Thread.__init__(self)

    def run(self):
        print 'Received connection:', self.details[0]
        self.channel.send('(Response)')
        print 'Received:', self.channel.recv(1024)
        self.channel.close()
        print 'Closed connection:', self.details[0]


class MyThreadedSocketServerHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        thread1 = MyServerThread(self.request, self.client_address)
        thread1.start()

if __name__ == '__main__':
    server = SocketServer.TCPServer(('localhost', 8888), MyThreadedSocketServerHandler)
    server.serve_forever()


#---Client to test---
import socket

message = "(Request)"

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sock.connect(('localhost', 8888))
    sock.send(message)
    data = sock.recv(1024)
    print 'Sent:     %s' % message
    print 'Received: %s' % data
finally:
    sock.close()




#Working example without "join"
import socket
import threading

class ClientThread(threading.Thread):

    def __init__(self, channel, details):
        self.channel = channel
        self.details = details
        threading.Thread.__init__(self)

    def run(self):
        print 'Received connection:', self.details[0]
        self.channel.send('message')
        print self.channel.recv(1024)
        self.channel.close()
        print 'Closed connection:', self.details[0]

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 8888))
server.listen(5)

while True:
    channel, details = server.accept()
    ClientThread(channel, details).start()

【问题讨论】:

  • 可能客户端在您尝试发送数据时关闭连接?

标签: python multithreading sockets


【解决方案1】:

我想你在找ThreadingMixIn:

from SocketServer import TCPServer, ThreadingMixIn

class MyServerThread(ThreadingMixIn, TCPServer): pass

【讨论】:

    【解决方案2】:

    你的问题是:

    thread1.start()
    

    上面的代码立即返回,MyServerThread.run() 在后台运行。所以 MyThreadedSocketServerHandler.handle() 在 MyServerThread.run() 可以执行之前返回。 handle() 函数返回后,self.request 将关闭,您的客户端将一无所获。执行 MyServerThread.run() 时,self.request.send() 将失败,因为套接字已关闭。

    所以添加:

    thread1.join()
    

    开始后

    【讨论】:

    • 好的。现在可以了。但是为什么它在另一个没有加入的例子中工作(我以不同的方式启动服务器)?查看更新的问题
    【解决方案3】:

    我认为问题在于你的线程处理接收到的数据,你的请求在线程可以做任何事情之前被关闭。

    例如,如果你添加一个

    thread1.join()
    

    你的客户看起来像这样

        def handle(self):
            thread1 = MyServerThread(self.request, self.client_address)
            thread1.start()
            thread1.join()
    

    你不会有错误。

    其实,在调用handle之后,Handler就关闭了连接。而您的问题是您的线程在关闭后尝试处理它。

    我希望我对你有所帮助,我的英语不好理解(我是法国人)

    【讨论】:

      猜你喜欢
      • 2011-12-04
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      相关资源
      最近更新 更多