# Julia interact with Python

**URL:** <https://discourse.julialang.org/t/julia-interact-with-python/89417>\
**Category:** New to Julia\
**Tags:** pycall\
**Created:** [October 28, 2022, 2:18am UTC](https://discourse.julialang.org/t/julia-interact-with-python/89417 "2022-10-28T02:18:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [October 28, 2022, 2:18am UTC](https://discourse.julialang.org/t/julia-interact-with-python/89417/1 "2022-10-28T02:18:44Z")

</div>

I have a python dict `a_dict`:

```julia
using PyCall
thread = pyimport("_thread")
py"""
from time import sleep
from random import randint, random
a_dict = {"A": 0}
def keepUpdate():
    while True:
        sleep(random())
        a_dict["A"] = randint(1, 20)
"""
thread.start_new_thread(py"keepUpdate", ())

```

The dict is keep changing. In Julia, I need to keep monitor the `a_dict`, and whenever `a_dict[" A"] == 3`, something would happen, for example:

```julia
function keep_watching(x)
    while true
        if x["A"] == 3
            println("the dict get 3")
        end
    end
end
Threads.@spawn keep_watching(py"a_dict")

```

But in the codes, the monitor part `while true` is consume too much computer sources. Is there a better way to do it?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 28, 2022, 2:24am UTC](https://discourse.julialang.org/t/julia-interact-with-python/89417/2 "2022-10-28T02:24:19Z")

</div>

this sounds like a very bad way of doting anything.

what’s your actual problem / objective?

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [October 28, 2022, 2:35am UTC](https://discourse.julialang.org/t/julia-interact-with-python/89417/3 "2022-10-28T02:35:00Z")

</div>

I have some data being updated in python codes. I need to call a julia function whenever the data finish a updating. The updating interval is not fixed, and I need very low latence bettween the updating time (finished in python code) and function call (finished in julia code). Actucally the function is just a kind of callback function.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [October 28, 2022, 1:49pm UTC](https://discourse.julialang.org/t/julia-interact-with-python/89417/4 "2022-10-28T13:49:47Z")

</div>

> [@Brian1](#):
>
> I need to call a julia function whenever the data finish a updating. The updating interval is not fixed,

make that python function blocking? I mean if you call a python function from Julia, it won’t return until it finishes.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 28, 2022, 2:22pm UTC](https://discourse.julialang.org/t/julia-interact-with-python/89417/5 "2022-10-28T14:22:38Z")

</div>

In particular, I would generally encourage you to think in terms of calling Python functions and methods from Julia rather than running Python “scripts” in Julia (and somehow monitoring them asynchronously). Function calls are generally the best way to pass data and results from one part of a program to another, whether in the same language or between languages.

On a separate note, Python is not thread-safe so it’s problematic to try to interact with Python running on one thread from another Julia thread.
