| 1 |
# Copyright 2016 gRPC authors. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
from concurrent import futures |
| 16 |
import threading |
| 17 |
|
| 18 |
|
| 19 |
class RecordingThreadPool(futures.ThreadPoolExecutor): |
| 20 |
"""A thread pool that records if used.""" |
| 21 |
|
| 22 |
def __init__(self, max_workers): |
| 23 |
self._tp_executor = futures.ThreadPoolExecutor(max_workers=max_workers) |
| 24 |
self._lock = threading.Lock() |
| 25 |
self._was_used = False |
| 26 |
|
| 27 |
def submit(self, fn, *args, **kwargs): # pylint: disable=arguments-differ |
| 28 |
with self._lock: |
| 29 |
self._was_used = True |
| 30 |
self._tp_executor.submit(fn, *args, **kwargs) |
| 31 |
|
| 32 |
def was_used(self): |
| 33 |
with self._lock: |
| 34 |
return self._was_used |