| 1 |
## Multiprocessing with gRPC Python |
| 2 |
|
| 3 |
Multiprocessing allows application developers to sidestep the Python global |
| 4 |
interpreter lock and achieve true parallelism on multicore systems. |
| 5 |
Unfortunately, using multiprocessing and gRPC Python is not yet as simple as |
| 6 |
instantiating your server with a `futures.ProcessPoolExecutor`. |
| 7 |
|
| 8 |
The library is implemented as a C extension, maintaining much of the state that |
| 9 |
drives the system in native code. As such, upon calling |
| 10 |
[`fork`](http://man7.org/linux/man-pages/man2/fork.2.html), any threads in a |
| 11 |
critical section may leave the state of the gRPC library invalid in the child |
| 12 |
process. See this [excellent research |
| 13 |
paper](https://www.microsoft.com/en-us/research/uploads/prod/2019/04/fork-hotos19.pdf) |
| 14 |
for a thorough discussion of the topic. |
| 15 |
|
| 16 |
Calling `fork` without `exec` in your process *is* supported |
| 17 |
before any gRPC servers have been instantiated. Application developers can |
| 18 |
take advantage of this to parallelize their CPU-intensive operations. |
| 19 |
|
| 20 |
## Calculating Prime Numbers with Multiple Processes |
| 21 |
|
| 22 |
This example calculates the first 10,000 prime numbers as an RPC. We instantiate |
| 23 |
one server per subprocess, balancing requests between the servers using the |
| 24 |
[`SO_REUSEPORT`](https://lwn.net/Articles/542629/) socket option. |
| 25 |
|
| 26 |
```python |
| 27 |
_PROCESS_COUNT = multiprocessing.cpu_count() |
| 28 |
``` |
| 29 |
|
| 30 |
On the server side, we detect the number of CPUs available on the system and |
| 31 |
spawn exactly that many child processes. If we spin up fewer, we won't be taking |
| 32 |
full advantage of the hardware resources available. |
| 33 |
|
| 34 |
## Running the Example |
| 35 |
|
| 36 |
To run the server, |
| 37 |
[ensure `bazel` is installed](https://docs.bazel.build/versions/master/install.html) |
| 38 |
and run: |
| 39 |
|
| 40 |
``` |
| 41 |
bazel run //examples/python/multiprocessing:server & |
| 42 |
``` |
| 43 |
|
| 44 |
Note the address at which the server is running. For example, |
| 45 |
|
| 46 |
``` |
| 47 |
... |
| 48 |
[PID 107153] Binding to '[::]:33915' |
| 49 |
[PID 107507] Starting new server. |
| 50 |
[PID 107508] Starting new server. |
| 51 |
... |
| 52 |
``` |
| 53 |
|
| 54 |
Note that several servers have been started, each with its own PID. |
| 55 |
|
| 56 |
Now, start the client by running |
| 57 |
|
| 58 |
``` |
| 59 |
bazel run //examples/python/multiprocessing:client -- [SERVER_ADDRESS] |
| 60 |
``` |
| 61 |
|
| 62 |
For example, |
| 63 |
|
| 64 |
``` |
| 65 |
bazel run //examples/python/multiprocessing:client -- [::]:33915 |
| 66 |
``` |
| 67 |
|
| 68 |
Alternatively, generate code using the following and then run the client and server |
| 69 |
directly: |
| 70 |
|
| 71 |
```python |
| 72 |
cd examples/python/helloworld |
| 73 |
python -m grpc_tools.protoc -I . prime.proto --python_out=. --grpc_python_out=. |
| 74 |
``` |