yokawasa / azure-functions-python-samples

Azure Functions Python Sample Codes. NOTE: The project, hosted in a repository, is no longer actively maintained by its creators or contributors. There won't be any further updates, bug fixes, or support from the original developers in the project.
MIT License
392 stars 181 forks source link

examples of output binding to service bus queue #25

Closed alexgman closed 5 years ago

alexgman commented 5 years ago

Is there a way to read/write from service bus queue using python? I know there might not be a way to using an explicit binding, but perhaps there's a manual way where we establish a connection and manually put a message on a service bus queue? My main interest is in triggering with servicebus and then output to servicebus

snobu commented 5 years ago

Just func new and pick the Service Bus template. Do you mean there's no output binding for Service Bus in the runtime?

alexgman commented 5 years ago

correct, there's no output binding

On Fri, Aug 2, 2019 at 11:52 AM Adrian Calinescu notifications@github.com wrote:

Just func new and pick the Service Bus template. Do you mean there's no output binding for Service Bus in the runtime?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/yokawasa/azure-functions-python-samples/issues/25?email_source=notifications&email_token=ABAIE3YVRT2AZUB4VLNPKKTQCRQ5FA5CNFSM4II7DMI2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3OJFMQ#issuecomment-517771954, or mute the thread https://github.com/notifications/unsubscribe-auth/ABAIE33PZUUGLCFTXYKGVYLQCRQ5FANCNFSM4II7DMIQ .

snobu commented 5 years ago

DEPRECATED, SEE MY NEXT COMMENT

Use a Storage Queue output binding for your main function and create a second function that triggers on that Storage Queue and writes to Service Bus using the storage SDK. This way, once there's a Service Bus output binding you only need to change configuration, not code. Of course you can do it all in one function if you wish, just a personal preference to separate concerns.

From https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-python-how-to-use-queues#send-messages-to-a-queue -

from azure.servicebus import QueueClient, Message

# Create the QueueClient
queue_client = QueueClient.from_connection_string(
    "<CONNECTION STRING>", "<QUEUE NAME>")

# Send a test message to the queue
msg = Message(b'Test Message')
queue_client.send(msg)

Don't forget to add azure.servicebus to your function app's requirements.txt.

snobu commented 5 years ago

Wait, judging by this there is an output binding for Service Bus. Try that first.

snobu commented 5 years ago

This PR (https://github.com/yokawasa/azure-functions-python-samples/pull/26) adds a sample for Service Bus trigger and output. I've tested it, the message gets added to the output Service Bus queue successfully.

yokawasa commented 5 years ago

@alexgman We close the issue ( See PR #26). Please feel free to open if you need further discussion!