Created
December 3, 2020 20:24
-
-
Save mfitton/a3e4f87921776d5a4a7ea519f5f43fe5 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 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)) | |
placeholder = st.empty() | |
i = 0 | |
while True: | |
params = ray.get(actor.get_streamlit_params.remote()) | |
with placeholder.beta_container(): | |
### USER CODE TO RENDER ON EVERY REFRESH GOES HERE | |
### WE PROBABLY WANT TO INSERT THE USER'S SCRIPT | |
### INSIDE THIS SCAFFOLDING. | |
st.write(f"iteration #{i}") | |
st.write("foo: ", params["foo"]) | |
st.write("bar: ", params["bar"]) | |
time.sleep(5) | |
i += 1 | |
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.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 get_streamlit_params(self): | |
return {"foo": self.foo, | |
"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