Skip to content

Commit

Permalink
changed the way that SocketNER retrieves data from the socket server;…
Browse files Browse the repository at this point in the history
… may address issue dat#14 and issue dat#11.
  • Loading branch information
erickpeirson committed May 3, 2015
1 parent d5e1563 commit 42ed5c8
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion ner/client.py
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

0 comments on commit 42ed5c8

Please sign in to comment.