在python中启动和停止StanfordCoreNLP服务器的标准方法?

时间:2017-09-19 10:44:08

标签: python-3.x subprocess stanford-nlp

我使用subprocess启动CoreNLP服务器,

  1. command = 'java -mx4g -cp "*;stanford-corenlp-full-2017-06-09/*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000'

  2. server = subprocess.Popen(command, stdout=subprocess.PIPE)

  3. 停止使用的服务器:

    1. server.kill()
    2. 第一次运行没有问题,

      如果我尝试再次启动上述所有步骤,则会出错:

        

      错误是:

          java.net.BindException: Address already in use: bind
          at sun.nio.ch.Net.bind0(Native Method)
          at sun.nio.ch.Net.bind(Unknown Source)
          at sun.nio.ch.Net.bind(Unknown Source)
          at sun.nio.ch.ServerSocketChannelImpl.bind(Unknown Source)
          at sun.nio.ch.ServerSocketAdaptor.bind(Unknown Source)
          at sun.net.httpserver.ServerImpl.<init>(Unknown Source)
          at sun.net.httpserver.HttpServerImpl.<init>(Unknown Source)
          at sun.net.httpserver.DefaultHttpServerProvider.createHttpServer(Unknown Source)
          at com.sun.net.httpserver.HttpServer.create(Unknown Source)
          at edu.stanford.nlp.pipeline.StanfordCoreNLPServer.run(StanfordCoreNLPServer.java:1277)
          at edu.stanford.nlp.pipeline.StanfordCoreNLPServer.main(StanfordCoreNLPServer.java:1369)
      
      任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

您应该使用shutdown键发送关闭消息。以下是命令行调用示例:

wget "localhost:9000/shutdown?key=`cat /tmp/corenlp.shutdown`" -O -

您可以使用subprocessos.system等执行此类命令...

请注意,除非您指定其他名称,否则关键项位于/tmp/corenlp.shutdown

如果你想要更好,你也可以使用requests库:

import requests

from commands import getoutput

url = "http://localhost:9000/shutdown?"
shutdown_key = getoutput("cat /tmp/corenlp.shutdown")
r = requests.post(url,data="",params={"key": shutdown_key})

这也会将关闭消息传送到服务器。