Skip to content

Commit 7a7db71

Browse files
Update examples to use asyncio event loop
The user had tried to instantiate the client outside the asyncio event loop, but it must be created within the loop to handle asynchronous calls correctly. This change updates the examples to use async functions and runs them within an asyncio event loop. Signed-off-by: Daniel Zullo <[email protected]>
1 parent 3e4c132 commit 7a7db71

File tree

1 file changed

+70
-50
lines changed

1 file changed

+70
-50
lines changed

src/frequenz/client/electricity_trading/__init__.py

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,21 @@
3636
???+ example "Initialize the client"
3737
3838
```python
39+
import asyncio
3940
from frequenz.client.electricity_trading import Client
4041
4142
# Change server address if needed
4243
SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
4344
with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
4445
API_KEY = f.read().strip()
45-
client = Client(
46-
server_url=SERVICE_URL,
47-
auth_key=API_KEY
48-
)
46+
47+
async def initialize():
48+
client = Client(
49+
server_url=SERVICE_URL,
50+
auth_key=API_KEY
51+
)
52+
53+
asyncio.run(initialize())
4954
```
5055
5156
### Example Usage
@@ -58,6 +63,7 @@
5863
???+ example "Create a limit order"
5964
6065
```python
66+
import asyncio
6167
from frequenz.client.electricity_trading import (
6268
Client,
6369
Currency,
@@ -76,32 +82,36 @@
7682
SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
7783
with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
7884
API_KEY = f.read().strip()
79-
client = Client(
80-
server_url=SERVICE_URL,
81-
auth_key=API_KEY
82-
)
8385
84-
# Define order parameters
85-
GRIDPOOL_ID = 1
86-
delivery_area = DeliveryArea(
87-
code="10YDE-EON------1", # TenneT
88-
code_type=EnergyMarketCodeType.EUROPE_EIC
89-
)
90-
delivery_period = DeliveryPeriod(
91-
start=datetime.fromisoformat("2024-05-01T00:00:00+00:00"),
92-
duration=timedelta(minutes=15)
93-
)
94-
price = Price(amount=Decimal("50.0"), currency=Currency.EUR)
95-
quantity = Energy(mwh=Decimal("0.1"))
96-
order = await client.create_gridpool_order(
97-
gridpool_id=GRIDPOOL_ID,
98-
delivery_area=delivery_area,
99-
delivery_period=delivery_period,
100-
order_type=OrderType.LIMIT,
101-
side=MarketSide.BUY,
102-
price=price,
103-
quantity=quantity,
104-
)
86+
async def create_order():
87+
client = Client(
88+
server_url=SERVICE_URL,
89+
auth_key=API_KEY
90+
)
91+
92+
# Define order parameters
93+
gridpool_id = 1
94+
delivery_area = DeliveryArea(
95+
code="10YDE-EON------1", # TenneT
96+
code_type=EnergyMarketCodeType.EUROPE_EIC
97+
)
98+
delivery_period = DeliveryPeriod(
99+
start=datetime.fromisoformat("2024-05-01T00:00:00+00:00"),
100+
duration=timedelta(minutes=15)
101+
)
102+
price = Price(amount=Decimal("50.0"), currency=Currency.EUR)
103+
quantity = Energy(mwh=Decimal("0.1"))
104+
order = await client.create_gridpool_order(
105+
gridpool_id=gridpool_id,
106+
delivery_area=delivery_area,
107+
delivery_period=delivery_period,
108+
order_type=OrderType.LIMIT,
109+
side=MarketSide.BUY,
110+
price=price,
111+
quantity=quantity,
112+
)
113+
114+
asyncio.run(create_order())
105115
```
106116
107117
#### List Orders for a Gridpool
@@ -110,29 +120,34 @@
110120
???+ example "List orders for a gridpool"
111121
112122
```python
123+
import asyncio
113124
from frequenz.client.electricity_trading import ( Client, MarketSide )
114125
115126
# Change server address if needed
116127
SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
117128
with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
118129
API_KEY = f.read().strip()
119-
client = Client(
120-
server_url=SERVICE_URL,
121-
auth_key=API_KEY
122-
)
123130
124-
gridpool_id: int = 1
131+
async def list_orders():
132+
client = Client(
133+
server_url=SERVICE_URL,
134+
auth_key=API_KEY
135+
)
125136
126-
# List all orders for a given gridpool
127-
orders = await client.list_gridpool_orders(
128-
gridpool_id=gridpool_id,
129-
)
137+
gridpool_id: int = 1
130138
131-
# List only the buy orders for a given gridpool
132-
buy_orders = await client.list_gridpool_orders(
133-
gridpool_id=gridpool_id,
134-
side=MarketSide.BUY,
135-
)
139+
# List all orders for a given gridpool
140+
orders = await client.list_gridpool_orders(
141+
gridpool_id=gridpool_id,
142+
)
143+
144+
# List only the buy orders for a given gridpool
145+
buy_orders = await client.list_gridpool_orders(
146+
gridpool_id=gridpool_id,
147+
side=MarketSide.BUY,
148+
)
149+
150+
asyncio.run(list_orders())
136151
```
137152
138153
@@ -142,19 +157,24 @@
142157
143158
???+ example "Stream public trades"
144159
```python
160+
import asyncio
145161
from frequenz.client.electricity_trading import Client
146162
147163
# Change server address if needed
148164
SERVICE_URL = "grpc://electricity-trading.api.frequenz.com:443?ssl=true"
149165
with open('/path/to/api_key.txt', 'r', encoding='utf-8') as f:
150166
API_KEY = f.read().strip()
151-
client = Client(
152-
server_url=SERVICE_URL,
153-
auth_key=API_KEY
154-
)
155-
stream_public_trades = await client.stream_public_trades()
156-
async for public_trade in stream_public_trades:
157-
print(f"Received public trade: {public_trade}")
167+
168+
async def stream_trades():
169+
client = Client(
170+
server_url=SERVICE_URL,
171+
auth_key=API_KEY
172+
)
173+
stream_public_trades = await client.stream_public_trades()
174+
async for public_trade in stream_public_trades:
175+
print(f"Received public trade: {public_trade}")
176+
177+
asyncio.run(stream_trades())
158178
```
159179
160180
"""

0 commit comments

Comments
 (0)