Fixed HttpClient session not being closed
This commit is contained in:
parent
300fba156e
commit
b388c43454
@ -28,6 +28,20 @@ class HttpClient:
|
|||||||
self.progress_hook = progress_hook
|
self.progress_hook = progress_hook
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, *args):
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self.session:
|
||||||
|
self.session.close()
|
||||||
|
self.session = None
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
self.close()
|
||||||
|
|
||||||
def post(self, url: str, data: Any, headers: Optional[Dict[str, str]]) -> Response:
|
def post(self, url: str, data: Any, headers: Optional[Dict[str, str]]) -> Response:
|
||||||
data = _MonitoringFile(
|
data = _MonitoringFile(
|
||||||
data, hook=self.progress_hook
|
data, hook=self.progress_hook
|
||||||
|
@ -803,6 +803,7 @@ to a cloze type first, via Edit>Change Note Type."""
|
|||||||
local = False
|
local = False
|
||||||
# fetch it into a temporary folder
|
# fetch it into a temporary folder
|
||||||
self.mw.progress.start(immediate=not local, parent=self.parentWindow)
|
self.mw.progress.start(immediate=not local, parent=self.parentWindow)
|
||||||
|
r = None
|
||||||
ct = None
|
ct = None
|
||||||
error_msg: Optional[str] = None
|
error_msg: Optional[str] = None
|
||||||
try:
|
try:
|
||||||
@ -825,6 +826,8 @@ to a cloze type first, via Edit>Change Note Type."""
|
|||||||
error_msg = _("An error occurred while opening %s") % e
|
error_msg = _("An error occurred while opening %s") % e
|
||||||
return
|
return
|
||||||
finally:
|
finally:
|
||||||
|
if r:
|
||||||
|
r.close()
|
||||||
self.mw.progress.finish()
|
self.mw.progress.finish()
|
||||||
if error_msg:
|
if error_msg:
|
||||||
showWarning(error_msg)
|
showWarning(error_msg)
|
||||||
|
Loading…
Reference in New Issue
Block a user