Skip to content

Commit a13e844

Browse files
MHHukiewitzhoh
authored andcommitted
Fix: /examples directory was unmaintained and did not work correctly
Solution: Debug examples and only use async client
1 parent 3a1b305 commit a13e844

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

examples/metrics.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
""" Server metrics upload.
22
"""
3-
# -*- coding: utf-8 -*-
4-
3+
import asyncio
54
import os
65
import platform
76
import time
@@ -12,9 +11,11 @@
1211
from aleph_message.status import MessageStatus
1312

1413
from aleph.sdk.chains.ethereum import get_fallback_account
15-
from aleph.sdk.client import AuthenticatedAlephClientSync, AuthenticatedAlephHttpClient
14+
from aleph.sdk.client import AuthenticatedAlephHttpClient
1615
from aleph.sdk.conf import settings
1716

17+
# -*- coding: utf-8 -*-
18+
1819

1920
def get_sysinfo():
2021
uptime = int(time.time() - psutil.boot_time())
@@ -53,10 +54,12 @@ def get_cpu_cores():
5354
return [c._asdict() for c in psutil.cpu_times_percent(0, percpu=True)]
5455

5556

56-
def send_metrics(
57-
session: AuthenticatedAlephClientSync, metrics
57+
async def send_metrics(
58+
session: AuthenticatedAlephHttpClient, metrics
5859
) -> Tuple[AlephMessage, MessageStatus]:
59-
return session.create_aggregate(key="metrics", content=metrics, channel="SYSINFO")
60+
return await session.create_aggregate(
61+
key="metrics", content=metrics, channel="SYSINFO"
62+
)
6063

6164

6265
def collect_metrics():
@@ -68,17 +71,17 @@ def collect_metrics():
6871
}
6972

7073

71-
def main():
74+
async def main():
7275
account = get_fallback_account()
73-
with AuthenticatedAlephHttpClient(
76+
async with AuthenticatedAlephHttpClient(
7477
account=account, api_server=settings.API_HOST
7578
) as session:
7679
while True:
7780
metrics = collect_metrics()
78-
message, status = send_metrics(session, metrics)
81+
message, status = await send_metrics(session, metrics)
7982
print("sent", message.item_hash)
8083
time.sleep(10)
8184

8285

8386
if __name__ == "__main__":
84-
main()
87+
asyncio.run(main())

examples/mqtt.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def get_input_data(value):
2626
return value.decode("utf-8")
2727

2828

29-
def send_metrics(account, metrics):
30-
with AuthenticatedAlephHttpClient(
29+
async def send_metrics(account, metrics):
30+
async with AuthenticatedAlephHttpClient(
3131
account=account, api_server=settings.API_HOST
3232
) as session:
3333
return session.create_aggregate(

0 commit comments

Comments
 (0)