stanfordnlp / stanza

Stanford NLP Python library for tokenization, sentence segmentation, NER, and parsing of many human languages
https://stanfordnlp.github.io/stanza/
Other
7.31k stars 896 forks source link

CoreNLP server doesn't close when using with-statements #953

Open songyang-dev opened 2 years ago

songyang-dev commented 2 years ago

Describe the bug The CoreNLP server is not stopped automatically after the with statement in Python is finished. This happens during interactive Python sessions and running Python scripts as a whole. The server will close itself if the terminal session is closed.

To Reproduce Steps to reproduce the behavior:

  1. Write
    
    from stanza.server import CoreNLPClient

if name == "main": text = "Chris Manning is a nice person. Chris wrote a simple sentence. He also gives oranges to people." with CoreNLPClient( annotators=[ "tokenize", "ssplit", "pos", "lemma", "ner", "parse", "depparse", "coref", ], timeout=30000, memory="6G", ) as client: ann = client.annotate(text)

2. Run this code as a script in the terminal
3. Run this code again as a script in the terminal
4. See error

***Second situation***
1. Write the same code.
2. Run the code interactively using `python -i`
3. Call `client.stop()`

**Expected behavior**
The server is supposed to be closed at the end of the script or upon calling `stop()`, according to this [demo](https://colab.research.google.com/github/stanfordnlp/stanza/blob/master/demo/Stanza_CoreNLP_Interface.ipynb#scrollTo=W435Lwc4YqKb).

**Environment (please complete the following information):**
 - OS: Win 11
 - Python version: Python 3.9.5 from Anaconda
 - Stanza version: 1.3.0

**Additional context**
I am running this inside VSCode.

**Error message**

$ py -3.9 -i extraction/preprocess.py 2022-02-11 17:52:07 INFO: Writing properties to tmp file: corenlp_server-f591e0f5098f4ab5.props Traceback (most recent call last): File "C:\Users\songy\miniconda3\lib\site-packages\stanza\server\client.py", line 132, in start sock.bind((self.host, self.port)) OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\songy\Documents\My Documents\UDEM\master thesis\uml data\database\cleaning\3step\extraction\preprocess.py", line 16, in with CoreNLPClient( File "C:\Users\songy\miniconda3\lib\site-packages\stanza\server\client.py", line 185, in enter self.start() File "C:\Users\songy\miniconda3\lib\site-packages\stanza\server\client.py", line 139, in start raise PermanentlyFailedException("Error: unable to start the CoreNLP server on port %d " stanza.server.client.PermanentlyFailedException: Error: unable to start the CoreNLP server on port 9000 (possibly something is already running there)

AngledLuffa commented 2 years ago

Is this some issue with Windows not freeing up the port quickly enough? I can run the server multiple times in a row and it works fine for me. No idea about VSCode, though.

One thought that has been in the back of my mind for a while is that there's no reason the functionality of the server can't just be in a subprocess pipe for people who need annotations from a local java process. Aside from the problem that editing the server code was the original inspiration for Dante's Inferno