| 1 |
# Copyright 2019 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 |
"""An example of multiprocessing concurrency with gRPC.""" |
| 15 |
|
| 16 |
from __future__ import absolute_import |
| 17 |
from __future__ import division |
| 18 |
from __future__ import print_function |
| 19 |
|
| 20 |
import argparse |
| 21 |
import atexit |
| 22 |
import logging |
| 23 |
import multiprocessing |
| 24 |
import operator |
| 25 |
import sys |
| 26 |
|
| 27 |
import grpc |
| 28 |
import prime_pb2 |
| 29 |
import prime_pb2_grpc |
| 30 |
|
| 31 |
_PROCESS_COUNT = 8 |
| 32 |
_MAXIMUM_CANDIDATE = 10000 |
| 33 |
|
| 34 |
# Each worker process initializes a single channel after forking. |
| 35 |
# It's regrettable, but to ensure that each subprocess only has to instantiate |
| 36 |
# a single channel to be reused across all RPCs, we use globals. |
| 37 |
_worker_channel_singleton = None |
| 38 |
_worker_stub_singleton = None |
| 39 |
|
| 40 |
_LOGGER = logging.getLogger(__name__) |
| 41 |
|
| 42 |
|
| 43 |
def _shutdown_worker(): |
| 44 |
_LOGGER.info('Shutting worker process down.') |
| 45 |
if _worker_channel_singleton is not None: |
| 46 |
_worker_channel_singleton.stop() |
| 47 |
|
| 48 |
|
| 49 |
def _initialize_worker(server_address): |
| 50 |
global _worker_channel_singleton # pylint: disable=global-statement |
| 51 |
global _worker_stub_singleton # pylint: disable=global-statement |
| 52 |
_LOGGER.info('Initializing worker process.') |
| 53 |
_worker_channel_singleton = grpc.insecure_channel(server_address) |
| 54 |
_worker_stub_singleton = prime_pb2_grpc.PrimeCheckerStub( |
| 55 |
_worker_channel_singleton) |
| 56 |
atexit.register(_shutdown_worker) |
| 57 |
|
| 58 |
|
| 59 |
def _run_worker_query(primality_candidate): |
| 60 |
_LOGGER.info('Checking primality of %s.', primality_candidate) |
| 61 |
return _worker_stub_singleton.check( |
| 62 |
prime_pb2.PrimeCandidate(candidate=primality_candidate)) |
| 63 |
|
| 64 |
|
| 65 |
def _calculate_primes(server_address): |
| 66 |
worker_pool = multiprocessing.Pool(processes=_PROCESS_COUNT, |
| 67 |
initializer=_initialize_worker, |
| 68 |
initargs=(server_address,)) |
| 69 |
check_range = range(2, _MAXIMUM_CANDIDATE) |
| 70 |
primality = worker_pool.map(_run_worker_query, check_range) |
| 71 |
primes = zip(check_range, map(operator.attrgetter('isPrime'), primality)) |
| 72 |
return tuple(primes) |
| 73 |
|
| 74 |
|
| 75 |
def main(): |
| 76 |
msg = 'Determine the primality of the first {} integers.'.format( |
| 77 |
_MAXIMUM_CANDIDATE) |
| 78 |
parser = argparse.ArgumentParser(description=msg) |
| 79 |
parser.add_argument('server_address', |
| 80 |
help='The address of the server (e.g. localhost:50051)') |
| 81 |
args = parser.parse_args() |
| 82 |
primes = _calculate_primes(args.server_address) |
| 83 |
print(primes) |
| 84 |
|
| 85 |
|
| 86 |
if __name__ == '__main__': |
| 87 |
handler = logging.StreamHandler(sys.stdout) |
| 88 |
formatter = logging.Formatter('[PID %(process)d] %(message)s') |
| 89 |
handler.setFormatter(formatter) |
| 90 |
_LOGGER.addHandler(handler) |
| 91 |
_LOGGER.setLevel(logging.INFO) |
| 92 |
main() |