FYI, here is a MWE
using PyCall
py"""
import sys
import time
import threading
def printing(n):
for i in range(n):
time.sleep(1)
print(f"i={i}")
th = threading.Thread(target=printing, args=(3,))
th.start()
"""
while py"th.is_alive()"
py"sys.stdout.flush()"
sleep(1)
end
py"sys.stdout.flush()"
By the way, you can’t swap main and background threads because calling Julia functions from different thread is not safe. So, it has to be done this way. But it won’t be hard to wrap this in a function.