简体   繁体   中英

How to do I send a message to a queue in Azure from an Azure function using python?

I am using an azure function that has a time trigger and I am trying to communication with a data base to return a list of dictionaries, I am trying to send each dictionary as a string (queue message) to a queue, I was going to do this with an output binder but could not figure out how to, so I am using the azure module. Problem is every message I send goes into a poison queue for some reason, and I cannot figure out why, here is a code snippet of what I have in my Azure function.

import os
import platform
import WorkWithDatabase
#import base64
from azure.storage.queue import QueueService
acc='...ACCOUNT NAME'
key='...KEY'

#Connect to QueueService 
queue_service = QueueService(account_name=acc, account_key=key)

#Pull missing data from the database,
#Call a function in another script to do this

missingList=WorkWithDatabase.ListRequests()

for item in missingList:
    queue_service.put_message('taskqueue', str(item))

Also Is there a way I can use the database as a resource in an azure function with python??

For using these python packages, such as pyodbc or pymssql to connect Azure SQL Database, you need to install a custom version of Python on Azure Function, then to install pip for the custom verion of Python to install these package as you want.

So the steps as below.

  1. Follow the document Using a custom version of Python to install a custom Python runtime in the path site\\tools of Kudu via access the url https://<your function name>.scm.azurewebsites.net/DebugConsole .
  2. After custom python installation, to download a get-pip.py file to the path site\\tool of custom Python, then to install pip tool via command python get-pip.py .
  3. Then, you can install these packages via Scripts/pip.exe install <package-names> .

Then, you can import these packages to write your code in your Azure Function on Azure portal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM