With JSON it really works perfect:
using KiteModels, KitePodModels, Oxygen, HTTP, StructTypes
set::Settings = deepcopy(se())
kcu::KCU = KCU(set)
kps4::KPS4 = KPS4(kcu)
# Add a supporting struct type definition so JSON3 can serialize & deserialize automatically
StructTypes.StructType(::Type{SysState}) = StructTypes.Struct()
function init()
global integrator
integrator = KiteModels.init_sim!(kps4, stiffness_factor=0.5, prn=true)
nothing
end
function start_server(log=true)
@get "/sys_state" function(req::HTTP.Request)
SysState(kps4)
end
@get "/init" function(req::HTTP.Request)
init()
end
# start the web server
if log
serve()
else
serve(access_log=nothing)
end
end
The Python client:
import http.client
import json
connection = http.client.HTTPConnection('127.0.0.1:8080')
def init():
connection.request('GET', '/init')
response = connection.getresponse()
obj = json.loads(response.read())
return obj
def sys_state():
connection.request('GET', '/sys_state')
response = connection.getresponse()
obj = json.loads(response.read())
return obj
print(init())
state = sys_state()
print(state)
And the performance is much better than I said in my last post, I did not benchmark correctly:
ipython
%timeit sys_state()
# result: 63.3 µs ± 3.12 µs per loop on Ryzen 7850X
# 99.9 µs ± 5.05 µs per loop on Laptop in performance mode
UPDATE: The final code is available here: GitHub - ufechner7/pykitemodels: Kite power system models for Python