Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Unified Diff: third_party/grpc/src/python/grpcio/grpc/framework/foundation/logging_pool.py

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/grpc/src/python/grpcio/grpc/framework/foundation/logging_pool.py
diff --git a/third_party/WebKit/Source/devtools/scripts/devtools_file_hashes.py b/third_party/grpc/src/python/grpcio/grpc/framework/foundation/logging_pool.py
old mode 100755
new mode 100644
similarity index 50%
copy from third_party/WebKit/Source/devtools/scripts/devtools_file_hashes.py
copy to third_party/grpc/src/python/grpcio/grpc/framework/foundation/logging_pool.py
index 8c67f1cf86b325b84481899090effab28aa969aa..f82c7f7fbaebf917f2e30cffd1a63ce5454f562c
--- a/third_party/WebKit/Source/devtools/scripts/devtools_file_hashes.py
+++ b/third_party/grpc/src/python/grpcio/grpc/framework/foundation/logging_pool.py
@@ -1,5 +1,5 @@
-#!/usr/bin/env python
-# Copyright (c) 2014 Google Inc. All rights reserved.
+# Copyright 2015-2016, Google Inc.
+# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
@@ -27,53 +27,56 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-import hashlib
-import os.path
+"""A thread pool that logs exceptions raised by tasks executed within it."""
-try:
- import json
-except ImportError:
- import simplejson as json
+import logging
+from concurrent import futures
-def save_hashes(hashes_file_path, hashes):
+
+def _wrap(behavior):
+ """Wraps an arbitrary callable behavior in exception-logging."""
+ def _wrapping(*args, **kwargs):
try:
- with open(hashes_file_path, "wt") as hashes_file:
- json.dump(hashes, hashes_file, indent=4, separators=(",", ": "))
- except:
- print "ERROR: Failed to write %s" % hashes_file_path
- raise
+ return behavior(*args, **kwargs)
+ except Exception as e:
+ logging.exception(
+ 'Unexpected exception from %s executed in logging pool!', behavior)
+ raise
+ return _wrapping
-def load_hashes(hashes_file_path):
- try:
- with open(hashes_file_path, "r") as hashes_file:
- hashes = json.load(hashes_file)
- except:
- return {}
- return hashes
-
-
-def calculate_file_hash(file_path):
- with open(file_path) as file:
- data = file.read()
- md5_hash = hashlib.md5(data).hexdigest()
- return md5_hash
-
-
-def files_with_invalid_hashes(hash_file_path, file_paths):
- hashes = load_hashes(hash_file_path)
- result = []
- for file_path in file_paths:
- file_name = os.path.basename(file_path)
- if calculate_file_hash(file_path) != hashes.get(file_name, ""):
- result.append(file_path)
- return result
-
-
-def update_file_hashes(hash_file_path, file_paths):
- hashes = {}
- for file_path in file_paths:
- file_name = os.path.basename(file_path)
- hashes[file_name] = calculate_file_hash(file_path)
- save_hashes(hash_file_path, hashes)
+class _LoggingPool(object):
+ """An exception-logging futures.ThreadPoolExecutor-compatible thread pool."""
+
+ def __init__(self, backing_pool):
+ self._backing_pool = backing_pool
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self._backing_pool.shutdown(wait=True)
+
+ def submit(self, fn, *args, **kwargs):
+ return self._backing_pool.submit(_wrap(fn), *args, **kwargs)
+
+ def map(self, func, *iterables, **kwargs):
+ return self._backing_pool.map(
+ _wrap(func), *iterables, timeout=kwargs.get('timeout', None))
+
+ def shutdown(self, wait=True):
+ self._backing_pool.shutdown(wait=wait)
+
+
+def pool(max_workers):
+ """Creates a thread pool that logs exceptions raised by the tasks within it.
+
+ Args:
+ max_workers: The maximum number of worker threads to allow the pool.
+
+ Returns:
+ A futures.ThreadPoolExecutor-compatible thread pool that logs exceptions
+ raised by the tasks executed within it.
+ """
+ return _LoggingPool(futures.ThreadPoolExecutor(max_workers))

Powered by Google App Engine
This is Rietveld 408576698