1

ポートをリッスンし、非常に単純なことを行うtwistdプラグインがあります。問題は、私がそれを開始したときに、投稿が利用できない場合、プロセスが実行されているだけで何もしていないことです。この場合、大規模なシステムが問題を認識して対処できるように、プロセスをすぐに終了する必要があります。

次のようなコードがあります。

def makeService(options):
    root = Resource() #  Not what I actually have...
    factory = server.Site(root)
    server_string = b'tcp:{0}:interface={1}'.format(options['port'], options['interface'])
    endpoint = endpoints.serverFromString(reactor, server_string)
    service = internet.StreamServerEndpointService(endpoint, factory)
    return service

これにより、次の結果が得られます。

2016-12-19T11:42:21-0600] [info] [3082] [-] Log opened.
[2016-12-19T11:42:21-0600] [info] [3082] [-] twistd 15.5.0 (/home/matthew/code-venvs/wgcbap/bin/python 2.7.6) starting up.
[2016-12-19T11:42:21-0600] [info] [3082] [-] reactor class: twisted.internet.epollreactor.EPollReactor.
[2016-12-19T11:42:21-0600] [critical] [3082] [-] Unhandled Error
        Traceback (most recent call last):
          File "/home/matthew/code-venvs/wgcbap/local/lib/python2.7/site-packages/twisted/scripts/_twistd_unix.py", line 394, in startApplication
            service.IService(application).privilegedStartService()
          File "/home/matthew/code-venvs/wgcbap/local/lib/python2.7/site-packages/twisted/application/service.py", line 278, in privilegedStartService
            service.privilegedStartService()
          File "/home/matthew/code-venvs/wgcbap/local/lib/python2.7/site-packages/twisted/application/internet.py", line 352, in privilegedStartService
            self._waitingForPort = self.endpoint.listen(self.factory)
          File "/home/matthew/code-venvs/wgcbap/local/lib/python2.7/site-packages/twisted/internet/endpoints.py", line 457, in listen
            interface=self._interface)
        --- <exception caught here> ---
          File "/home/matthew/code-venvs/wgcbap/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 121, in execute
            result = callable(*args, **kw)
          File "/home/matthew/code-venvs/wgcbap/local/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 478, in listenTCP
            p.startListening()
          File "/home/matthew/code-venvs/wgcbap/local/lib/python2.7/site-packages/twisted/internet/tcp.py", line 984, in startListening
            raise CannotListenError(self.interface, self.port, le)
        twisted.internet.error.CannotListenError: Couldn't listen on 127.0.0.1:9999: [Errno 98] Address already in use.

そして何もせずに走り続ける……。

service._raiseSynchronously = Truereturn のすぐ上に行を追加すると動作しますが、文書化されていないようで、汚いと感じます。

これを行う承認された方法はありますか?

4

0 に答える 0