Created
December 8, 2020 17:59
-
-
Save mfitton/25d3a88a96dc68b87fbd2062e024f706 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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