Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python SDK Performance Improvement #5

Merged
merged 4 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The **KuCoin Universal SDK** is the official SDK provided by KuCoin, offering a
### Python Installation

```bash
pip install kucoin-universal-sdk==0.1.1a
pip install kucoin-universal-sdk==0.1.1a1
```

### Golang Installation
Expand Down Expand Up @@ -119,7 +119,7 @@ For other languages, refer to the [Examples](#-examples) section.

## 📚 Documentation

- Official Documentation: [KuCoin API Docs](https://www.kucoin.com/docs-new/doc-338144)
- Official Documentation: [KuCoin API Docs](https://www.kucoin.com/docs-new)
- **[Python Documentation](sdk/python/README.md)**
- **[Go Documentation](sdk/golang/README.md)**

Expand Down
5 changes: 4 additions & 1 deletion sdk/golang/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Go SDK Documentation
![License Badge](https://img.shields.io/badge/license-MIT-green)
![Language](https://img.shields.io/badge/Go-blue)

Welcome to the **Go** implementation of the KuCoin Universal SDK. This SDK is built based on KuCoin API specifications to provide a comprehensive and optimized interface for interacting with the KuCoin platform.

For an overview of the project and SDKs in other languages, refer to the [Main README](https://github.com/kucoin/kucoin-universal-sdk).
Expand Down Expand Up @@ -79,7 +82,7 @@ func example() {
}
```
## 📚 Documentation
Official Documentation: [KuCoin API Docs](https://www.kucoin.com/docs)
Official Documentation: [KuCoin API Docs](https://www.kucoin.com/docs-new)

## 📂 Examples

Expand Down
7 changes: 5 additions & 2 deletions sdk/python/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Python SDK Documentation
![License Badge](https://img.shields.io/badge/license-MIT-green)
![Language](https://img.shields.io/badge/Python-blue)

Welcome to the **Python** implementation of the KuCoin Universal SDK. This SDK is built based on KuCoin API specifications to provide a comprehensive and optimized interface for interacting with the KuCoin platform.

For an overview of the project and SDKs in other languages, refer to the [Main README](https://github.com/kucoin/kucoin-universal-sdk).
Expand All @@ -9,7 +12,7 @@ For an overview of the project and SDKs in other languages, refer to the [Main R
Install the Python SDK using `pip`:

```bash
pip install kucoin-universal-sdk==0.1.1a
pip install kucoin-universal-sdk==0.1.1a1
```

## 📖 Getting Started
Expand Down Expand Up @@ -80,7 +83,7 @@ if __name__ == "__main__":

```
## 📚 Documentation
Official Documentation: [KuCoin API Docs](https://www.kucoin.com/docs)
Official Documentation: [KuCoin API Docs](https://www.kucoin.com/docs-new)

## 📂 Examples

Expand Down
33 changes: 20 additions & 13 deletions sdk/python/kucoin_universal_sdk/internal/infra/default_ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,24 @@ def write_message(self):
def keep_alive(self):
interval = self.token_info.ping_interval / 1000.0
timeout = self.token_info.ping_timeout / 1000.0
last_ping_time = time.time()

while not self.shutdown.is_set() and not self.close_event.is_set():
time.sleep(interval)
ping_msg = self.new_ping_message()
try:
self.write(ping_msg, timeout=timeout)
self.metric['ping_success'] += 1
except TimeoutError:
logging.error("Heartbeat ping timeout")
self.metric['ping_err'] += 1
except Exception as e:
logging.error(f"Exception in keep_alive: {e}")
self.metric['ping_err'] += 1
current_time = time.time()
if current_time - last_ping_time >= interval:
ping_msg = self.new_ping_message()
try:
self.write(ping_msg, timeout=timeout)
self.metric['ping_success'] += 1
except TimeoutError:
logging.error("Heartbeat ping timeout")
self.metric['ping_err'] += 1
except Exception as e:
logging.error(f"Exception in keep_alive: {e}")
self.metric['ping_err'] += 1
last_ping_time = current_time

time.sleep(1)

def on_error(self, ws, error):
logging.error(f"WebSocket error: {error}")
Expand All @@ -219,10 +225,10 @@ def on_close(self, ws, close_status_code, close_msg):
def reconnect(self):
def reconnect_loop():
while True:
if self.reconnect_close_event.is_set():
if self.reconnect_close_event.wait(timeout=1):
return

if self.disconnect_event.is_set():
if self.disconnect_event.wait(timeout=1):
if self.shutdown.is_set():
continue

Expand Down Expand Up @@ -287,6 +293,7 @@ def close(self):
self.conn = None
self.close_event.set()
logging.info("WebSocket connection closed.")
logging.info("Waiting all threads close...")
self.token_provider.close()
self.write_thread.join()
self.keep_alive_thread.join()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def __init__(self, client_option: ClientOption, domain: DomainType, private: boo

def recovery(self):
def recovery_loop():
while not self.stop_event.is_set():
event_triggered = self.client.reconnected_event.wait(timeout=1)
while not self.stop_event.wait(timeout=1):
event_triggered = self.client.reconnected_event.is_set()
if self.stop_event.is_set():
return
if event_triggered:
Expand Down
2 changes: 1 addition & 1 deletion sdk/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="kucoin-universal-sdk",
version="0.1.1a",
version="0.1.1a1",
description="Official KuCoin Universal SDK",
author="KuCoin",
author_email="[email protected]",
Expand Down