Skip to content

Instantly share code, notes, and snippets.

@mfitton
Created December 3, 2020 19:27
Show Gist options
  • Save mfitton/7e5d164491a0e48abad4be1bb31343ac to your computer and use it in GitHub Desktop.
Save mfitton/7e5d164491a0e48abad4be1bb31343ac to your computer and use it in GitHub Desktop.
import ray
import os
import time
from datetime import datetime, timedelta
import streamlit as st
def main():
ray.init(address="auto")
actor_name: Optional[str] = os.getenv("ACTOR_NAME")
if not actor_name:
raise ValueError("Must supply a valid actor name through the ACTOR_NAME environment variable.")
actor = retry_until_success(lambda: ray.get_actor(actor_name))
while True:
st.write("calling remote")
actor.render_streamlit.remote()
time.sleep(5)
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.foo = 6
self.bar = 8
def do_task(self):
for i in range(100):
if i % 2 == 0:
self.foo *= 2
self.bar *= 2.1
else:
self.foo /= 1.9
self.bar /= 2
return self.foo, self.bar
def render_streamlit(self):
st.write("FOO ", self.foo)
st.write("BAR ", self.bar)
def main():
ray.init()
sta = StreamLitActor.options(name="streamlitactor").remote()
while True:
res = sta.do_task.remote()
time.sleep(2)
print(ray.get(res))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment