Skip to content

changed the way that SocketNER retrieves data from the socket server #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: python
python:
- "2.7"
script: python run_tests

23 changes: 22 additions & 1 deletion ner/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import json
import re
import time
import socket

from itertools import groupby
Expand Down Expand Up @@ -123,7 +124,27 @@ def tag_text(self, text):
if not isinstance(text, bytes):
text = text.encode('utf-8')
s.sendall(text)
tagged_text = s.recv(10*len(text))

timeout = 2 # Consider making this configurable?
lastResponse = time.time()
tagged_text = '' # Response content will be appended as it is received.
while True:
# If some data has already been received, bail after `timeout` seconds.
if len(tagged_text) > 0 and time.time() - lastResponse > timeout:
break

# Wait a bit longer if no data has been received, just in case.
elif time.time() - lastResponse > timeout * 2:
break

data = s.recv(4092) # Retrieve a small chunk of the response.
if data:
tagged_text += data
lastResponse = time.time()
else: # Wait a bit before looking again.
time.sleep(0.1)


return tagged_text.decode('utf-8')


Expand Down