Skip to content

Commit 7c22a57

Browse files
author
Marcin Kardas
committed
Close GROBID connection after request
1 parent 80a31e5 commit 7c22a57

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

sota_extractor2/data/references.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,16 @@ def migrate(self):
7575
def _post(self, data):
7676
tries = 0
7777
while tries < self.max_tries:
78-
r = requests.post(f'http://{self.host}:{self.port}/api/processCitation', data=data)
78+
with requests.Session() as s:
79+
r = s.post(
80+
f'http://{self.host}:{self.port}/api/processCitation',
81+
data=data,
82+
headers={'Connection': 'close'}
83+
)
84+
if r.status_code in [200, 204]:
85+
return r.content.decode("utf-8")
7986
if r.status_code != 503:
80-
return r
87+
raise RuntimeError(f"{r.status_code} {r.reason}\n{r.content}")
8188
tries += 1
8289
if tries < self.max_tries:
8390
time.sleep(self.retry_wait)
@@ -87,8 +94,8 @@ def parse_ref_str_to_tei_dict(self, ref_str):
8794
cache = self.get_cache()
8895
d = cache.get(ref_str)
8996
if d is None: # potential multiple recomputation in multithreading case
90-
r = self._post(data={'citations': ref_str})
91-
d = xmltodict.parse(r.content.decode("utf-8"))
97+
content = self._post(data={'citations': ref_str})
98+
d = xmltodict.parse(content)
9299
d = to_normal_dict(d)
93100
cache[ref_str] = d
94101
return d

0 commit comments

Comments
 (0)