我有一些 Python 脚本用于对数据库进行查询,然后将这些数据提供给对 REST API 端点进行 http 调用的工作人员池。我想尝试将这些作为 Azure 函数应用程序运行,但要做到这一点,我需要将所有内容都放在一个函数中。这是我当前脚本的简化示例:
def httpRequest(record):
data = record.getCTJson()
response = requests.put('https://api.this.com/v2/users/'
headers={'Content-Type': 'application/json'},
data=data)
return response
def processResults(results):
#Do some logging
if __name__ == '__main__':
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};' + connectionString)
cnxn.autocommit = True
cursor = cnxn.cursor()
sql = "select myData from myTable"
cursor.execute(sql)
row = cursor.fetchone()
records = []
# Looping over the cursor to get all the records, building a list of purchases along with a hash table
while row:
records.append(record(row.myData))
row = cursor.fetchone()
pool = multiprocessing.Pool(processes=40)
results = pool.map_async(httpRequest, records)
pool.close()
pool.join()
processResults(results)
但我需要在这样的函数中运行它:
import azure.functions as func
def main(mytimer: func.TimerRequest) -> None:
#Do all of your stuff
我立即遇到的问题是如果name == ' main ' 处理不进入:我打印name并且它是“ app ”。加上函数名:" app .myfunction
如果我使用name == " app .myfunction" 代替,我可以得到处理输入,但这当然会导致问题,当它到达池对象时,我会收到错误 "ModuleNotFoundError: No module named ' app '"。
有没有办法在这样的 Azure Function App 中使用池化,还是我需要重新考虑整个方法?