Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何以编程方式杀死扭曲的websocket服务器_Python_Python 3.x_Macos_Twisted_Autobahn - Fatal编程技术网

Python 如何以编程方式杀死扭曲的websocket服务器

Python 如何以编程方式杀死扭曲的websocket服务器,python,python-3.x,macos,twisted,autobahn,Python,Python 3.x,Macos,Twisted,Autobahn,如何以编程方式杀死websocket服务器?我将把这个服务器和其他东西一起部署到生产环境中。我喜欢构建一个python脚本,它向所有东西发送一个kill信号。我不知道如何在没有用户键盘中断或kill-9的情况下杀死这个东西 sys.exit()不起作用 psutil和terminate()也不起作用 import os import psutil current_system_pid = os.getpid() ThisSystem = psutil.Process(current_syst

如何以编程方式杀死websocket服务器?我将把这个服务器和其他东西一起部署到生产环境中。我喜欢构建一个python脚本,它向所有东西发送一个kill信号。我不知道如何在没有用户键盘中断或kill-9的情况下杀死这个东西

sys.exit()不起作用

psutil和terminate()也不起作用

import os
import psutil

current_system_pid = os.getpid()

ThisSystem = psutil.Process(current_system_pid)
ThisSystem.terminate()
我没有主意了。现在我用kill-9在命令行上杀死它

当我以各种方式杀死它时,它往往会看到下面的信息,但纸条仍在运行

2020-12-12 12:24:54-0500 [autobahn.twisted.websocket.WebSocketServerFactory] (TCP Port 8080 Closed)
2020-12-12 12:24:54-0500 [-] Stopping factory <autobahn.twisted.websocket.WebSocketServerFactory object at 0x110680f28>
代码:

from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory
import sys
from twisted.python import log
from twisted.internet import reactor

class MyServerProtocol(WebSocketServerProtocol):

    def onConnect(self, request):
        print("Client connecting: {0}".format(request.peer))

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        print("Text message received: {0}".format(payload.decode('utf8')))

        # echo back message verbatim
        # self.sendMessage(payload, isBinary)

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {0}".format(reason))


def StopWebsocketServer():
    PrintAndLog_FuncNameHeader("Begin")
    reactor.stop()
    PrintAndLog_FuncNameHeader("End")


if __name__ == '__main__':   
    # TODO remove the logging that came in the example
    log.startLogging(sys.stdout)

    factory = WebSocketServerFactory("ws://127.0.0.1:8080")
    factory.protocol = MyServerProtocol

    # note to self: if using putChild, the child must be bytes...
    reactor.listenTCP(Port_ws, factory)
    reactor.run()
解决方案使用@Jean-Paul Calderone的答案:

    import os
    import signal
    os.kill(os.getpid(), signal.SIGKILL)

我有一个外部python脚本,它向我的每个python脚本发送一个kill信号。kill信号只是每个脚本都知道要查找的文件的存在。一旦该终止信号出现,每个脚本都知道它在终止之前有x秒的时间。通过这种方式,他们有几秒钟的时间优雅地完成一些事情。

twisted.internet.reactor.stop()
是导致反应堆关闭的方式。这通常会导致基于Twisted的程序退出(当然,如果程序在反应堆关闭后做更多的事情,当然不一定要退出,但这并不常见)

但是,听起来您不想知道在进程内运行什么Python代码来结束它。您想知道其他进程可以对基于Twisted的进程执行哪些操作以使其退出。您给出了两种解决方案-键盘中断和SIGKILL。你没有提到为什么这两种解决方案都不合适。我觉得它们很好

如果您对SIGKILL感到不舒服(毕竟,您不应该这样做,因为许多原因,您的程序可能会不合时宜地消亡,您应该准备好处理这个问题)那么,关于KeyboardInterrupt,您可能忽略了一点,那就是它只是Python程序中由默认的SIGINT处理程序引发的异常


如果您将SIGINT发送到基于Twisted的进程,那么在正常使用情况下,这将停止反应堆并允许有序关闭。

您考虑过吗?os.kill确实与signal.SIGKILL一起工作。谢谢
信号。SIGKILL
工作得很好。谢谢我确实尝试过reactor.stop(),出于某种原因,它停止了websocket服务器,但没有退出脚本。它表现得好像它仍然卡在一个循环中,但我没有任何循环。有可能我做错了什么,但西格基尔做的正是我想做的,所以我会同意。
    import os
    import signal
    os.kill(os.getpid(), signal.SIGKILL)