You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: daprdocs/content/en/developing-applications/building-blocks/pubsub/subscription-methods.md
+106-1Lines changed: 106 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -203,7 +203,112 @@ As messages are sent to the given message handler code, there is no concept of r
203
203
204
204
The example below shows the different ways to stream subscribe to a topic.
205
205
206
-
{{< tabs Go>}}
206
+
{{< tabs Python Go >}}
207
+
208
+
{{% codetab %}}
209
+
210
+
You can use the `subscribe` method, which returns a `Subscription` object and allows you to pull messages from the stream by calling the `next_message` method. This runs in and may block the main thread while waiting for messages.
211
+
212
+
```python
213
+
import time
214
+
215
+
from dapr.clients import DaprClient
216
+
from dapr.clients.grpc.subscription import StreamInactiveError
217
+
218
+
counter = 0
219
+
220
+
221
+
def process_message(message):
222
+
global counter
223
+
counter += 1
224
+
# Process the message here
225
+
print(f'Processing message: {message.data()} from {message.topic()}...')
print('No message received within timeout period.')
248
+
continue
249
+
250
+
# Process the message
251
+
response_status = process_message(message)
252
+
253
+
if response_status == 'success':
254
+
subscription.respond_success(message)
255
+
elif response_status == 'retry':
256
+
subscription.respond_retry(message)
257
+
elif response_status == 'drop':
258
+
subscription.respond_drop(message)
259
+
260
+
finally:
261
+
print("Closing subscription...")
262
+
subscription.close()
263
+
264
+
265
+
if __name__ == '__main__':
266
+
main()
267
+
268
+
```
269
+
270
+
You can also use the `subscribe_with_handler` method, which accepts a callback function executed for each message received from the stream. This runs in a separate thread, so it doesn't block the main thread.
271
+
272
+
```python
273
+
import time
274
+
275
+
from dapr.clients import DaprClient
276
+
from dapr.clients.grpc._response import TopicEventResponse
277
+
278
+
counter = 0
279
+
280
+
281
+
def process_message(message):
282
+
# Process the message here
283
+
global counter
284
+
counter += 1
285
+
print(f'Processing message: {message.data()} from {message.topic()}...')
286
+
return TopicEventResponse('success')
287
+
288
+
289
+
def main():
290
+
with (DaprClient() as client):
291
+
# This will start a new thread that will listen for messages
292
+
# and process them in the `process_message` function
0 commit comments