# What would be a nice way to call Julia from Python using a different process?

**URL:** https://discourse.julialang.org/t/what-would-be-a-nice-way-to-call-julia-from-python-using-a-different-process/117183
**Category:** General Usage
**Tags:** question, interoperability, python, rpc
**Created:** [July 18, 2024, 11:48am UTC](https://discourse.julialang.org/t/what-would-be-a-nice-way-to-call-julia-from-python-using-a-different-process/117183 "2024-07-18T11:48:23Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)
#### Post date: [July 18, 2024, 3:25pm UTC](https://discourse.julialang.org/t/what-would-be-a-nice-way-to-call-julia-from-python-using-a-different-process/117183/7 "2024-07-18T15:25:46Z")

</div>

With JSON it really works perfect:

```julia
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:

```julia
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:

```julia
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](https://github.com/ufechner7/pykitemodels)

---

_[View the full topic](https://discourse.julialang.org/t/what-would-be-a-nice-way-to-call-julia-from-python-using-a-different-process/117183)._
