Skip to content

Commit

Permalink
Fixed issue with async IO
Browse files Browse the repository at this point in the history
  • Loading branch information
SanPen committed Sep 26, 2024
1 parent 75c000b commit bd50554
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 80 deletions.
133 changes: 70 additions & 63 deletions .idea/workspace.xml

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ An example on how to send a grid from a script to the server:

```python
import os
import asyncio
import GridCalEngine.api as gce

# path to your file
Expand All @@ -1026,9 +1027,11 @@ gce.get_certificate(base_url="https://localhost:8000",
pwd="")

# send json
reply_from_server = gce.send_json_data(json_data=model_data,
endpoint_url="https://localhost:8000/upload",
certificate=gce.get_certificate_path())
reply_from_server = asyncio.get_event_loop().run_until_complete(
gce.send_json_data(json_data=model_data,
endpoint_url="https://localhost:8000/upload",
certificate=gce.get_certificate_path())
)

print(reply_from_server)
```
Expand Down
54 changes: 54 additions & 0 deletions pics/GridCal_yt.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ nptyping
windpowerlib
pvlib
requests
urllib3
pytest>=7.2.0
rdflib
pymoo
Expand Down
9 changes: 6 additions & 3 deletions src/GridCal/Session/server_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import time
import requests
import asyncio
from urllib3 import disable_warnings, exceptions
from typing import Callable, Dict, Union, List, Any
from PySide6.QtCore import QThread, Signal
Expand Down Expand Up @@ -305,9 +306,11 @@ def send_data(self, circuit: MultiCircuit, instruction: RemoteInstruction) -> No
if self.is_running():
model_data = gather_model_as_jsons_for_communication(circuit=circuit, instruction=instruction)

response = send_json_data(json_data=model_data,
endpoint_url=websocket_url,
certificate=self._certificate_path)
response = asyncio.get_event_loop().run_until_complete(
send_json_data(json_data=model_data,
endpoint_url=websocket_url,
certificate=self._certificate_path)
)

self.get_jobs()

Expand Down
14 changes: 6 additions & 8 deletions src/GridCalEngine/IO/gridcal/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def gather_model_as_jsons_for_communication(circuit: MultiCircuit,
return data


def send_json_data(json_data: Dict[str, Union[str, Dict[str, Dict[str, str]]]],
async def send_json_data(json_data: Dict[str, Union[str, Dict[str, Dict[str, str]]]],
endpoint_url: str,
certificate: str) -> Any:
"""
Expand All @@ -217,13 +217,11 @@ def send_json_data(json_data: Dict[str, Union[str, Dict[str, Dict[str, str]]]],
:return service response
"""

response = asyncio.get_event_loop().run_until_complete(
requests.post(
url=endpoint_url,
json=json_data,
stream=True,
verify=certificate
)
response = requests.post(
url=endpoint_url,
json=json_data,
stream=True,
verify=certificate
)

# return server response
Expand Down
9 changes: 6 additions & 3 deletions src/trunk/io/server_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
pwd="")

# send json
reply_from_server = gce.send_json_data(json_data=model_data,
endpoint_url="https://localhost:8000/upload",
certificate=gce.get_certificate_path())
reply_from_server = asyncio.get_event_loop().run_until_complete(
gce.send_json_data(json_data=model_data,
endpoint_url="https://localhost:8000/upload",
certificate=gce.get_certificate_path())
)


print(reply_from_server)

0 comments on commit bd50554

Please sign in to comment.