如何杀死SocketServer? [英] How to kill a SocketServer?

查看:67
本文介绍了如何杀死SocketServer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个带有线程混合的SocketServer,并且您需要运行serve_forever
。你怎么能把它关闭呢,或者更确切地说,它怎么可以关闭它自己呢?即使你使用handle_request循环

而不是serve_forever,它似乎仍然很难:


class myserver(ThreadingMixIn,TCPServer):传递

server = myserver(...)

server.shutdown = False

而不是server.shutdown:

server.handle_request(请求处理程序中的某些代码最终将server.shutdown设置为

True(例如从客户端接收shutdown命令),但是通过

然后主线程已被阻止等待另一个

连接。在设置关闭标志后,我最终让quit操作实际打开了一个

环回连接,只是为了取消阻塞套接字监听器的
。但这似乎很疯狂。


还有更好的办法吗?

Let''s say you have a SocketServer with the threading mix-in and you
run serve_forever on it. How can you shut it down, or rather, how can
it even shut itself down? Even if you use a handle_request loop
instead of serve_forever, it still seems difficult:

class myserver(ThreadingMixIn, TCPServer): pass
server = myserver(...)
server.shutdown = False
while not server.shutdown:
server.handle_request()

Some code in the request handler eventually sets server.shutdown to
True (say on receiving a "shutdown" command from the client), but by
then the main thread is already blocked waiting for another
connection. I ended up having the quit operation actually open a
loopback connection after setting the shutdown flag, just to unblock
the socket listener. But that seems insane.

Is there a better way?

推荐答案

Paul Rubin写道:
Paul Rubin wrote:
假设你有一个带有线程混合的SocketServer,你就可以运行serve_forever。你怎么能把它关闭,或者更确切地说,它怎么能自己关闭呢?
Let''s say you have a SocketServer with the threading mix-in and you
run serve_forever on it. How can you shut it down, or rather, how can
it even shut itself down?




我看了CherryPy的服务器,因为我知道它使用Python的'

BaseHTTPServer,它源自SocketServer,我知道

它是多线程的。


转除非我做了更多的研究,否则我不太了解它可以做出合理的b
总结。这里有一些文字

来自_cphttpserver


类PooledThreadServer(SocketServer.TCPServer):


allow_reuse_address = 1


"""使用工作线程池的TCP服务器。这优于Python标准库提供的

替代方案,它只提供一次处理单个请求的
(1),(2)处理每个请求

a单独的线程(通过ThreadingMixIn),或(3)处理

a单独进程中的每个请求(通过ForkingMixIn)。它在某些方面也优于Twisted使用的纯异步方法,因为它允许更多

直接和简单的编程模型面对阻塞

请求(即你不必为Deferreds而烦恼)。"""


这意味着它走了一条不同的路线。看起来像是一个

请求进入工作队列。客户从中获得
。有一个特殊的工作队列项目告诉

要停止的线程。


Andrew
da *** @ dalkescientific.com



I looked at CherryPy''s server because I know it uses Python''s
BaseHTTPServer which is derived from SocketServer, and I know
it''s multithreaded.

Turns out I don''t know enough about it to make a reasonable
summary, unless I do a lot more research. Here''s some text
from _cphttpserver

class PooledThreadServer(SocketServer.TCPServer):

allow_reuse_address = 1

"""A TCP Server using a pool of worker threads. This is superior to the
alternatives provided by the Python standard library, which only offer
(1) handling a single request at a time, (2) handling each request in
a separate thread (via ThreadingMixIn), or (3) handling each request in
a separate process (via ForkingMixIn). It''s also superior in some ways
to the pure async approach used by Twisted because it allows a more
straightforward and simple programming model in the face of blocking
requests (i.e. you don''t have to bother with Deferreds)."""

which means it went a different route. Looks like a
request comes in and is put to a work queue. Clients get
from it. There''s a special work queue item to tell
the threads to stop.

Andrew
da***@dalkescientific.com




Paul>假设您有一个带有线程混合的SocketServer和

Paul>你运行serve_forever。怎么能把它关闭,或者更确切地说,

Paul>它甚至可以自行关闭?即使你使用了

Paul> handle_request循环而不是serve_forever,它似乎仍然是

Paul>困难:


Paul> class myserver(ThreadingMixIn,TCPServer):传递

Paul> server = myserver(...)

Paul> server.shutdown = False

Paul>而不是server.shutdown:

Paul> server.handle_request()


Paul>请求处理程序中的一些代码最终设置server.shutdown

Paul>为True(比如从客户端收到关闭命令),

Paul>但到那时主线程已被阻止等待另一个

Paul>连接。


我正好用这个方案(我认为*)没问题。我看到的唯一可能是

的重要区别是我将ThreadingMixin子类化为

创建守护程序线程:


类DaemonThreadMixIn( SocketServer.ThreadingMixIn):

def process_request_thread(self,request,client_address):

试试:

self.finish_request(request,client_address)

self.close_request(请求)

除外:

self.handle_error(request,client_address)

self.close_request (请求)


def process_request(self,request,client_address):

t = threading.Thread(target = self.process_request_thread,

args =(request,client_address))

t.setDaemon(1)

t.start()


class GenericServer(DaemonThreadMixIn,SocketServer.TCPServer):

...


跳过


(*)也许是我的在我实际设置

server.shutdown为True之后,XML-RPC服务器实际上接受了一个连接退出。我将不得不看看



Paul> Let''s say you have a SocketServer with the threading mix-in and
Paul> you run serve_forever on it. How can you shut it down, or rather,
Paul> how can it even shut itself down? Even if you use a
Paul> handle_request loop instead of serve_forever, it still seems
Paul> difficult:

Paul> class myserver(ThreadingMixIn, TCPServer): pass
Paul> server = myserver(...)
Paul> server.shutdown = False
Paul> while not server.shutdown:
Paul> server.handle_request()

Paul> Some code in the request handler eventually sets server.shutdown
Paul> to True (say on receiving a "shutdown" command from the client),
Paul> but by then the main thread is already blocked waiting for another
Paul> connection.

I use precisely that scheme with (I think *) no problem. The only maybe
significant difference I see is that I subclass ThreadingMixin so that it
creates daemon threads:

class DaemonThreadMixIn(SocketServer.ThreadingMixIn):
def process_request_thread(self, request, client_address):
try:
self.finish_request(request, client_address)
self.close_request(request)
except:
self.handle_error(request, client_address)
self.close_request(request)

def process_request(self, request, client_address):
t = threading.Thread(target = self.process_request_thread,
args = (request, client_address))
t.setDaemon(1)
t.start()

class GenericServer(DaemonThreadMixIn, SocketServer.TCPServer):
...

Skip

(*) Maybe my XML-RPC server actually accepts one more connection after I set
server.shutdown to True before it actually exits. I''ll have to take a look
at that.


Skip Montanaro< sk ** @ pobox.com>写道:
Skip Montanaro <sk**@pobox.com> writes:
我正好用这个方案(我认为*)没问题。我看到的唯一可能是显着的区别是我将ThreadingMixin子类化,以便它创建守护程序线程:...


根据文档,你不要不需要子类,你可以设置

" daemon_threads"服务器上的属性,例如


class myserver(ThreadingMixIn,TCPServer):传递

server = myserver(...)

server.daemon_threads = True

server.serve_forever()

如上所述,您必须在实际启动服务器之前设置标志。

(*)也许我的XML-RPC服务器在实际退出之前将
server.shutdown设置为True后实际上又接受了一个连接。我必须看看
I use precisely that scheme with (I think *) no problem. The only maybe
significant difference I see is that I subclass ThreadingMixin so that it
creates daemon threads: ...
According to the docs, you don''t need the subclass, you can just set the
"daemon_threads" property on the server, e.g.

class myserver(ThreadingMixIn, TCPServer): pass
server = myserver(...)
server.daemon_threads = True
server.serve_forever()

You have to set the flag before you actually start the server, as above.
(*) Maybe my XML-RPC server actually accepts one more connection after I set
server.shutdown to True before it actually exits. I''ll have to take a look
at that.




是的,这正是我要问的。你怎么把服务器拿走?b $ b消失而不用再次连接到它?如果它停留在你身边,你不是要注意吗?



Yes, this is precisely what I''m asking. How do you get the server to
go away without going out of your way to connect to it again? Don''t you
notice if it stays around?


这篇关于如何杀死SocketServer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆