Skip to content

Instantly share code, notes, and snippets.

@mfitton
Created December 8, 2020 17:59
Show Gist options
  • Save mfitton/25d3a88a96dc68b87fbd2062e024f706 to your computer and use it in GitHub Desktop.
Save mfitton/25d3a88a96dc68b87fbd2062e024f706 to your computer and use it in GitHub Desktop.
import ray
import streamlit as st
def fetch_actor():
actor = retry_until_success(lambda: ray.get_actor("streamlitactor"))
return actor
def main():
ray.init(address="auto", ignore_reinit_error=True)
actor = fetch_actor()
x = st.slider("foo", 0, 10)
ray.get(actor.set_x.remote(x))
actor_x = ray.get(actor.get_x.remote())
# Render current streamlit slider value of x
# as well as actor's value of x
st.write("streamlit x: ", x)
st.write("actor x: ", actor_x["x"])
# Helper function to fetch actor in case first calls
# fail.
def retry_until_success(f, timeout=15):
end = datetime.now() + timedelta(seconds=timeout)
while True:
try:
r = f()
return r
except Exception as e:
if datetime.now() < end:
time.sleep(1)
continue
raise e
if __name__ == '__main__':
main()
import streamlit as st
import ray
import time
@ray.remote
class StreamLitActor:
def __init__(self):
self.x = 1
def set_x(self, x):
self.x = x
def get_x(self):
return self.x
def main():
ray.init()
sta = StreamLitActor.options(streamlit_script_path="./ray_streamlit_actor.py", name="streamlitactor").remote()
time.sleep(1000)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment