# Python

{% hint style="info" %}
Hackle Python SDK는 Python 3 이상을 지원합니다.

WSGI 환경을 사용하시는 경우 가이드 [WSGI 설정](https://docs.hackle.io/development-guide/python/python-sdk-wsgi-init) 을 꼭 확인해주세요
{% endhint %}

## 의존성 추가

[![](https://img.shields.io/pypi/v/hackle-sdk)](https://pypi.org/project/hackle-sdk)

```shell
pip install hackle-sdk
```

## SDK 초기화

{% hint style="danger" %}
hackle\_client는 전역변수이며, 반드시 한 번만 생성해야 합니다.

hackle\_client는 쓰레드에서 I/O없이 바로 결과를 평가하기 위해 내부적으로 상태를 관리합니다.\
이를 위해 추가적인 리소스를 사용합니다.

모든 요청에 대해 새로운 인스턴스를 생성하지 않고, 이미 생성되어 있는 인스턴스를 사용합니다.

hackle\_client 는 싱글톤(Singleton) 객체로 만들어져 있기 때문에, 다른 함수에서 사용할 때 hackle.Client()로 사용해도 재생성하지 않습니다.
{% endhint %}

`hackle_client`는 SDK 기능을 사용하기 위한 메소드들을 제공하는 클래스입니다.

SDK를 사용하기 위해서는 `hackle_client` 초기화가 필요합니다.

#### 인스턴스화

SDK 키를 전달하여 `hackle_client`를 인스턴스화 합니다.\
`hackle_client`는 필요한 정보들을 얻기 위해 백그라운드 작업으로 핵클 서버와 주기적으로 동기화합니다.

* SDK 키는 핵클 서비스의 대시보드 안에 위치한 [SDK 연동 정보](https://dashboard.hackle.io/config/sdk-setting)에서 확인하실 수 있습니다.

```python
from hackle import hackle

# YOUR_SERVER_SDK_KEY 자리에 SDK 키를 넣습니다.
hackle_client = hackle.Client(sdk_key=YOUR_SERVER_SDK_KEY)
```

## 종료

어플리케이션이 종료될 때 `hackle_client.close()` 메소드를 통해 `hackle_client`를 종료시켜야 합니다.\
이 과정을 통해 사용 중인 리소스를 반납하고 남아있는 이벤트를 전송합니다.

{% hint style="danger" %}
`hackle_client.close()` 호출 없이 어플리케이션이 종료되면 이벤트가 누락될 수 있습니다.
{% endhint %}

```python
hackle_client.close()
```

#### Flask나 Django를 사용 중 이라면?

`@atexit.register`를 통해 어플리케이션 종료 시 자동으로 `hackle_client`를 종료시킬 수 있습니다.

```python
import atexit

@atexit.register
def __exit__():
    hackle_client.close()
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hackle.io/development-guide/python.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
