mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Cancellable md5sum calculation on thread, Ref. gui#2239
This commit is contained in:
1 parent
6868e20a70
commit
dc377165f2
4 files changed
+47
-2
No files matched your search
@@ -20,6 +20,9 @@ import functools
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
import threading
|
||||
|
||||
from asyncio.futures import CancelledError
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
@@ -40,6 +43,25 @@ def wait_run_in_executor(func, *args, **kwargs):
|
||||
return future.result()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def cancellable_wait_run_in_executor(func, *args, **kwargs):
|
||||
"""
|
||||
Run blocking code in a different thread and wait
|
||||
for the result. Support cancellation.
|
||||
|
||||
:param func: Run this function in a different thread
|
||||
:param args: Parameters of the function
|
||||
:param kwargs: Keyword parameters of the function
|
||||
:returns: Return the result of the function
|
||||
"""
|
||||
stopped_event = threading.Event()
|
||||
kwargs['stopped_event'] = stopped_event
|
||||
try:
|
||||
yield from wait_run_in_executor(func, *args, **kwargs)
|
||||
except CancelledError:
|
||||
stopped_event.set()
|
||||
|
||||
|
||||
@asyncio.coroutine
|
||||
def subprocess_check_output(*args, cwd=None, env=None, stderr=False):
|
||||
"""
|
||||
|
||||
@@ -143,11 +143,13 @@ def images_directories(type):
|
||||
return [force_unix_path(p) for p in paths if os.path.exists(p)]
|
||||
|
||||
|
||||
def md5sum(path):
|
||||
def md5sum(path, stopped_event=None):
|
||||
"""
|
||||
Return the md5sum of an image and cache it on disk
|
||||
|
||||
:param path: Path to the image
|
||||
:param stopped_event: In case you execute this function on thread and would like to have possibility
|
||||
to cancel operation pass the `threading.Event`
|
||||
:returns: Digest of the image
|
||||
"""
|
||||
|
||||
@@ -167,6 +169,9 @@ def md5sum(path):
|
||||
m = hashlib.md5()
|
||||
with open(path, 'rb') as f:
|
||||
while True:
|
||||
if stopped_event is not None and stopped_event.is_set():
|
||||
log.error("MD5 sum calculation of `{}` has stopped due to cancellation".format(path))
|
||||
return
|
||||
buf = f.read(128)
|
||||
if not buf:
|
||||
break
|
||||
|
||||
Reference in new issue
Block a user