Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Ciri Kegigihan Azure: Senarai Pemprosesan

Ciri Kegigihan Azure: Senarai Pemprosesan

王林
王林ke hadapan
2024-02-22 12:25:10915semak imbas

Ciri Kegigihan Azure: Senarai Pemprosesan

Kandungan soalan

Saya mempunyai fungsi persisten biru yang ditulis dalam python dengan penyelaras dan dua fungsi aktif

orchestrator memanggil fungsi aktif pertama dan menerima sebagai balasan pembolehubah senarai (senarai nama dan senarai ini boleh menjadi dinamik setiap kali fungsi itu dilaksanakan)

Langkah seterusnya ialah memanggil fungsi aktiviti kedua untuk setiap item senarai (pemprosesan berurutan - disebabkan oleh pengehadan api panggilan fungsi aktiviti kedua)

#dynamically gets generated by the first activity function
payload=[1,2,3,4]            

tasks = [context.call_activity("secondfunction",ps) for ps in payload]
output = yield context.task_all(tasks)

Saya menggunakan sesuatu selain bersiri dalam kaedah fanout, tetapi saya nampaknya tidak dapat mencari alternatif untuk apa yang saya cuba lakukan.

Selain itu, dalam fail host.json, saya cuba memaksa bahawa hanya satu fungsi aktif boleh dijalankan pada masa tertentu untuk mengelakkan pemprosesan selari

"extensions": {
    "durableTask": {
      "maxConcurrentActivityFunctions": 1,
      "maxConcurrentOrchestratorFunctions": 1
    }
  }

Juga perlu diperhatikan ialah saya tidak boleh menghantar keseluruhan senarai ke fungsi aktiviti, seolah-olah saya melaksanakan fungsi aktiviti ia akan mengambil masa lebih daripada 5-10 minit, iaitu had tamat masa untuk fungsi azure, jadi cuba ulangi fungsi orkestrasi senarai

Tetapi hasilnya tidak berterusan

Terima kasih banyak atas maklum balas anda


Jawapan yang betul


Anda boleh cuba menggunakan dua kaedah berikut untuk mencapai keperluan anda:-

Kaedah 1:-

My function_app.py:-

import azure.functions as func
import azure.durable_functions as df

myapp = df.dfapp(http_auth_level=func.authlevel.anonymous)

# http starter
@myapp.route(route="orchestrators/{functionname}")
@myapp.durable_client_input(client_name="client")
async def http_start(req: func.httprequest, client):
    function_name = req.route_params.get('functionname')
    instance_id = await client.start_new(function_name, none)  # pass the functionname here
    response = client.create_check_status_response(req, instance_id)
    return response

# orchestrator
@myapp.orchestration_trigger(context_name="context")
def hello_orchestrator(context):
    cities = ["seattle", "tokyo", "london"]

    tasks = []
    for city in cities:
        tasks.append(context.call_activity("hello", city))

    # wait for all tasks to complete
    results = yield context.task_all(tasks)

    return results

# activity
@myapp.activity_trigger(input_name="city")
def hello(city: str):
    print(f"processing {city}...")
    # your activity function logic goes here
    result = f"hello {city}!"

    return result

Output:-

url fungsi:-

http://localhost:7071/api/orchestrators/hello_orchestrator

Kaedah 2:-

function_app.py:-

import azure.functions as func
import azure.durable_functions as df

myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)

# HTTP Starter
@myApp.route(route="orchestrators/{functionName}")
@myApp.durable_client_input(client_name="client")
async def http_start(req: func.HttpRequest, client):
    function_name = req.route_params.get('functionName')
    instance_id = await client.start_new(function_name, None)  # Pass the functionName here
    response = client.create_check_status_response(req, instance_id)
    return response

# Orchestrator
@myApp.orchestration_trigger(context_name="context")
def hello_orchestrator(context):
    # Call the first activity to get a list of names
    names_list = yield context.call_activity("get_names")

    # Process each name sequentially using the second activity
    results = []
    for name in names_list:
        result = yield context.call_activity("process_name", name)
        results.append(result)

    return results

# First Activity
@myApp.activity_trigger
def get_names():
    # Your logic to retrieve a dynamic list of names goes here
    # For demonstration purposes, returning a hardcoded list
    return ["John", "Alice", "Bob"]

# Second Activity
@myApp.activity_trigger(input_name="name")
def process_name(name: str):
    print(f"Processing {name}...")
    # Your logic to process each name goes here
    result = f"Hello {name}!"

    return result

Atas ialah kandungan terperinci Ciri Kegigihan Azure: Senarai Pemprosesan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:stackoverflow.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam