Skip to content

Commit 1d418a4

Browse files
authored
Merge pull request #4407 from hhunter-ms/issue_3321
Add non-SDK Python code example for Bulk Subscribe
2 parents 1d7c51b + d6034fb commit 1d418a4

File tree

1 file changed

+46
-4
lines changed
  • daprdocs/content/en/developing-applications/building-blocks/pubsub

1 file changed

+46
-4
lines changed

daprdocs/content/en/developing-applications/building-blocks/pubsub/pubsub-bulk.md

+46-4
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,13 @@ Status | Description
336336
`RETRY` | Message to be retried by Dapr
337337
`DROP` | Warning is logged and message is dropped
338338

339-
Please refer [Expected HTTP Response for Bulk Subscribe]({{< ref pubsub_api.md >}}) for further insights on response.
339+
Refer to [Expected HTTP Response for Bulk Subscribe]({{< ref pubsub_api.md >}}) for further insights on response.
340340

341341
### Example
342342

343-
Please refer following code samples for how to use Bulk Subscribe:
344-
345-
{{< tabs "Java" "JavaScript" ".NET" >}}
343+
The following code examples demonstrate how to use Bulk Subscribe.
346344

345+
{{< tabs "Java" "JavaScript" ".NET" "Python" >}}
347346
{{% codetab %}}
348347

349348
```java
@@ -471,7 +470,50 @@ public class BulkMessageController : ControllerBase
471470

472471
{{% /codetab %}}
473472

473+
{{% codetab %}}
474+
Currently, you can only bulk subscribe in Python using an HTTP client.
475+
476+
```python
477+
import json
478+
from flask import Flask, request, jsonify
479+
480+
app = Flask(__name__)
481+
482+
@app.route('/dapr/subscribe', methods=['GET'])
483+
def subscribe():
484+
# Define the bulk subscribe configuration
485+
subscriptions = [{
486+
"pubsubname": "pubsub",
487+
"topic": "TOPIC_A",
488+
"route": "/checkout",
489+
"bulkSubscribe": {
490+
"enabled": True,
491+
"maxMessagesCount": 3,
492+
"maxAwaitDurationMs": 40
493+
}
494+
}]
495+
print('Dapr pub/sub is subscribed to: ' + json.dumps(subscriptions))
496+
return jsonify(subscriptions)
497+
498+
499+
# Define the endpoint to handle incoming messages
500+
@app.route('/checkout', methods=['POST'])
501+
def checkout():
502+
messages = request.json
503+
print(messages)
504+
for message in messages:
505+
print(f"Received message: {message}")
506+
return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
507+
508+
if __name__ == '__main__':
509+
app.run(port=5000)
510+
511+
```
512+
513+
{{% /codetab %}}
514+
474515
{{< /tabs >}}
516+
475517
## How components handle publishing and subscribing to bulk messages
476518

477519
For event publish/subscribe, two kinds of network transfers are involved.

0 commit comments

Comments
 (0)