Debian Codegraph @

examples/python/cancellation/README.md

raw
1 ### Cancellation
2
3 In the example, we implement a silly algorithm. We search for bytestrings whose
4 hashes are similar to a given search string. For example, say we're looking for
5 the string "doctor". Our algorithm may return `JrqhZVkTDoctYrUlXDbL6pfYQHU=` or
6 `RC9/7mlM3ldy4TdoctOc6WzYbO4=`. This is a brute force algorithm, so the server
7 performing the search must be conscious of the resources it allows to each client
8 and each client must be conscientious of the resources it demands of the server.
9
10 In particular, we ensure that client processes cancel the stream explicitly
11 before terminating and we ensure that server processes cancel RPCs that have gone on longer
12 than a certain number of iterations.
13
14 #### Cancellation on the Client Side
15
16 A client may cancel an RPC for several reasons. Perhaps the data it requested
17 has been made irrelevant. Perhaps you, as the client, want to be a good citizen
18 of the server and are conserving compute resources.
19
20 ##### Cancelling a Server-Side Unary RPC from the Client
21
22 The default RPC methods on a stub will simply return the result of an RPC.
23
24 ```python
25 >>> stub = hash_name_pb2_grpc.HashFinderStub(channel)
26 >>> stub.Find(hash_name_pb2.HashNameRequest(desired_name=name))
27 <hash_name_pb2.HashNameResponse object at 0x7fe2eb8ce2d0>
28 ```
29
30 But you may use the `future()` method to receive an instance of `grpc.Future`.
31 This interface allows you to wait on a response with a timeout, add a callback
32 to be executed when the RPC completes, or to cancel the RPC before it has
33 completed.
34
35 In the example, we use this interface to cancel our in-progress RPC when the
36 user interrupts the process with ctrl-c.
37
38 ```python
39 stub = hash_name_pb2_grpc.HashFinderStub(channel)
40 future = stub.Find.future(hash_name_pb2.HashNameRequest(desired_name=name))
41 def cancel_request(unused_signum, unused_frame):
42 future.cancel()
43 sys.exit(0)
44 signal.signal(signal.SIGINT, cancel_request)
45
46 result = future.result()
47 print(result)
48 ```
49
50 We also call `sys.exit(0)` to terminate the process. If we do not do this, then
51 `future.result()` with throw an `RpcError`. Alternatively, you may catch this
52 exception.
53
54
55 ##### Cancelling a Server-Side Streaming RPC from the Client
56
57 Cancelling a Server-side streaming RPC is even simpler from the perspective of
58 the gRPC API. The default stub method is already an instance of `grpc.Future`,
59 so the methods outlined above still apply. It is also a generator, so we may
60 iterate over it to yield the results of our RPC.
61
62 ```python
63 stub = hash_name_pb2_grpc.HashFinderStub(channel)
64 result_generator = stub.FindRange(hash_name_pb2.HashNameRequest(desired_name=name))
65 def cancel_request(unused_signum, unused_frame):
66 result_generator.cancel()
67 sys.exit(0)
68 signal.signal(signal.SIGINT, cancel_request)
69 for result in result_generator:
70 print(result)
71 ```
72
73 We also call `sys.exit(0)` here to terminate the process. Alternatively, you may
74 catch the `RpcError` raised by the for loop upon cancellation.
75
76
77 #### Cancellation on the Server Side
78
79 A server is responsible for cancellation in two ways. It must respond in some way
80 when a client initiates a cancellation, otherwise long-running computations
81 could continue indefinitely.
82
83 It may also decide to cancel the RPC for its own reasons. In our example, the
84 server can be configured to cancel an RPC after a certain number of hashes has
85 been computed in order to conserve compute resources.
86
87 ##### Responding to Cancellations from a Servicer Thread
88
89 It's important to remember that a gRPC Python server is backed by a thread pool
90 with a fixed size. When an RPC is cancelled, the library does *not* terminate
91 your servicer thread. It is your responsibility as the application author to
92 ensure that your servicer thread terminates soon after the RPC has been
93 cancelled.
94
95 In this example, we use the `ServicerContext.add_callback` method to set a
96 `threading.Event` object when the RPC is terminated. We pass this `Event` object
97 down through our hashing algorithm and ensure to check that the RPC is still
98 ongoing before each iteration.
99
100 ```python
101 stop_event = threading.Event()
102 def on_rpc_done():
103 # Regain servicer thread.
104 stop_event.set()
105 context.add_callback(on_rpc_done)
106 secret = _find_secret(stop_event)
107 ```
108
109 ##### Initiating a Cancellation on the Server Side
110
111 Initiating a cancellation from the server side is simpler. Just call
112 `ServicerContext.cancel()`.
113
114 In our example, we ensure that no single client is monopolizing the server by
115 cancelling after a configurable number of hashes have been checked.
116
117 ```python
118 try:
119 for candidate in secret_generator:
120 yield candidate
121 except ResourceLimitExceededError:
122 print("Cancelling RPC due to exhausted resources.")
123 context.cancel()
124 ```
125
126 In this type of situation, you may also consider returning a more specific error
127 using the [`grpcio-status`](https://pypi.org/project/grpcio-status/) package.