본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Dapr 워크플로 Python SDK 시작하기

원문 보기 위키 갱신

Dapr Python SDK로 워크플로를 시작하고 실행하는 방법을 알아봐요. 콘솔을 사용해서 Dapr 워크플로를 만들고 호출해봐요. 제공된 워크플로 예제를 통해 여러분은:

  • 활동, 하위 워크플로, 외부 이벤트를 사용한 워크플로 오케스트레이션을 보여주는 Python 콘솔 애플리케이션을 실행해요
  • 재시도, 타임아웃, 워크플로 상태 관리 처리 방법을 배워요
  • Python 워크플로 SDK를 사용해서 워크플로 인스턴스를 시작, 일시중지, 재개, 제거(purge)해요

이 예제는 self-hosted 모드의 dapr init 기본 구성을 사용해요. Python 예제 프로젝트에서 simple.py 파일은 다음을 포함한 앱 설정을 담고 있어요:

  • 워크플로 정의
  • 워크플로 활동 정의
  • 워크플로와 워크플로 활동의 등록

출처: Dapr 문서

본문

사전 준비사항 (Prerequisites)

  • Dapr CLI 설치
  • 초기화된 Dapr 환경
  • Python 3.9+ 설치
  • Dapr Python 패키지와 워크플로 확장 설치
  • 최신 proto 바인딩을 사용하고 있는지 확인해요

환경 설정하기

Python SDK 레포를 클론하는 것으로 시작해요.

git clone https://github.com/dapr/python-sdk.git

Python SDK 루트 디렉터리에서 Dapr 워크플로 예제로 이동해요.

cd examples/workflow

Dapr Python SDK로 이 워크플로 예제를 실행하는 데 필요한 요구사항을 설치하려면 다음 명령을 실행해요.

pip3 install -r workflow/requirements.txt

애플리케이션을 로컬에서 실행하기

Dapr 애플리케이션을 실행하려면 Python 프로그램과 Dapr 사이드카를 시작해야 해요. 터미널에서 실행해요:

dapr run --app-id wf-simple-example --dapr-grpc-port 50001 --resources-path components -- python3 simple.py

참고: Windows에는 Python3.exe가 정의되어 있지 않으므로 python3 simple.py 대신 python simple.py를 사용해야 할 수 있어요.

예상 출력 (Expected output)

- "== APP == Hi Counter!"
- "== APP == New counter value is: 1!"
- "== APP == New counter value is: 11!"
- "== APP == Retry count value is: 0!"
- "== APP == Retry count value is: 1! This print statement verifies retry"
- "== APP == Appending 1 to child_orchestrator_string!"
- "== APP == Appending a to child_orchestrator_string!"
- "== APP == Appending a to child_orchestrator_string!"
- "== APP == Appending 2 to child_orchestrator_string!"
- "== APP == Appending b to child_orchestrator_string!"
- "== APP == Appending b to child_orchestrator_string!"
- "== APP == Appending 3 to child_orchestrator_string!"
- "== APP == Appending c to child_orchestrator_string!"
- "== APP == Appending c to child_orchestrator_string!"
- "== APP == Get response from hello_world_wf after pause call: Suspended"
- "== APP == Get response from hello_world_wf after resume call: Running"
- "== APP == New counter value is: 111!"
- "== APP == New counter value is: 1111!"
- "== APP == Workflow completed! Result: "Completed"

어떤 일이 일어났나요? (What happened?)

애플리케이션을 실행하면 몇 가지 핵심 워크플로 기능이 보여요:

  • 워크플로와 활동 등록: 애플리케이션은 Python 데코레이터를 사용해서 워크플로와 활동을 런타임에 자동으로 등록해요. 이 데코레이터 기반 접근 방식은 워크플로 컴포넌트를 정의하는 깔끔하고 선언적인 방법을 제공해요:
@wfr.workflow(name='hello_world_wf')
def hello_world_wf(ctx: DaprWorkflowContext, wf_input):


@wfr.activity(name='hello_act')
def hello_act(ctx: WorkflowActivityContext, wf_input):
  • 런타임 설정: 애플리케이션은 워크플로 런타임과 클라이언트를 초기화해요:
wfr = WorkflowRuntime()
wfr.start()
wf_client = DaprWorkflowClient()
  • 활동 실행: 워크플로는 카운터를 증가시키는 일련의 활동을 실행해요:
@wfr.workflow(name='hello_world_wf')
def hello_world_wf(ctx: DaprWorkflowContext, wf_input):
yield ctx.call_activity(hello_act, input=1)
yield ctx.call_activity(hello_act, input=10)
  • 재시도 로직: 워크플로는 재시도 정책으로 오류 처리를 보여줘요:
retry_policy = RetryPolicy(
first_retry_interval=timedelta(seconds=1),
max_number_of_attempts=3,
backoff_coefficient=2,
max_retry_interval=timedelta(seconds=10),
retry_timeout=timedelta(seconds=100),
)
yield ctx.call_activity(hello_retryable_act, retry_policy=retry_policy)
  • 하위 워크플로: 하위 워크플로가 자체 재시도 정책으로 실행돼요:
yield ctx.call_child_workflow(child_retryable_wf, retry_policy=retry_policy)
  • 외부 이벤트 처리: 워크플로는 타임아웃을 두고 외부 이벤트를 기다려요:
event = ctx.wait_for_external_event(event_name)
timeout = ctx.create_timer(timedelta(seconds=30))
winner = yield when_any([event, timeout])
  • 워크플로 수명주기 관리: 예제는 워크플로를 일시중지하고 재개하는 방법을 보여줘요:
wf_client.pause_workflow(instance_id=instance_id)
metadata = wf_client.get_workflow_state(instance_id=instance_id)

wf_client.resume_workflow(instance_id=instance_id)
  • 이벤트 발생: 재개 후 워크플로는 이벤트를 발생시켜요:
wf_client.raise_workflow_event(
instance_id=instance_id,
event_name=event_name,
data=event_data
)
  • 완료와 정리: 마지막으로 워크플로는 완료를 기다리고 정리해요:
state = wf_client.wait_for_workflow_completion(
instance_id,
timeout_in_seconds=30
)
wf_client.purge_workflow(instance_id=instance_id)

다음 단계 (Next steps)

  • Dapr 워크플로에 대해 더 알아보기
  • 워크플로 API 레퍼런스
  • 더 복잡한 워크플로 패턴 구현 시도하기

더 알아보기 (Learn more)