データベースに対してクエリを実行し、REST API エンドポイントへの http 呼び出しを行うワーカーのプールにそのデータを渡すために使用する Python スクリプトがいくつかあります。これらを Azure Function Apps として実行してみたいのですが、そのためにはすべてを関数内に配置する必要があります。現在のスクリプトの簡単な例を次に示します。
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
すぐに遭遇したトラブルは if name == ' main ': I print name and it's app .という処理が入らないことです。プラス関数名: " app .myfunction
代わりに if name == " app .myfunction"を使えば入る処理が入るのですが、これではもちろん問題が発生し、プールオブジェクトに到達すると"ModuleNotFoundError: No module named ' app '" というエラーが発生します。
このように Azure 関数アプリ内でプーリングを使用する方法はありますか? それとも、このアプローチ全体を再考する必要がありますか?