Skip to content

Commit

Permalink
move to legacy: server
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtruong committed Aug 27, 2024
1 parent 4626a60 commit 0339298
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion weave/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def cli() -> None:
# def start_ui() -> None:
# print("Starting server...")
# try:
# from weave import server
# from weave.legacy import server
# except ModuleNotFoundError:
# print("Run 'pip install weave[engine]' to use the local server.")
# sys.exit(1)
Expand Down
12 changes: 6 additions & 6 deletions weave/legacy/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@contextlib.contextmanager
def execution_client():
"""Returns a client for use by the execution engine and op resolvers."""
from weave import server
from weave.legacy import server

# Force in process execution
with context_state.client(client.NonCachingClient(server.InProcessServer())):
Expand All @@ -22,7 +22,7 @@ def execution_client():

@contextlib.contextmanager
def local_http_client():
from weave import server
from weave.legacy import server

s = server.HttpServer()
s.start()
Expand All @@ -34,7 +34,7 @@ def local_http_client():

@contextlib.contextmanager
def weavejs_client():
from weave import server
from weave.legacy import server

s = server.HttpServer()
s.start()
Expand All @@ -48,7 +48,7 @@ def use_fixed_server_port():
# s = server.HttpServer(port=9994)
# s.start()
# _weave_client.set(server.HttpServerClient(s.url))
from weave import server
from weave.legacy import server

context_state.set_client(server.HttpServerClient("http://localhost:9994"))

Expand All @@ -70,7 +70,7 @@ def use_lazy_execution():


def _make_default_client():
from weave import server
from weave.legacy import server

if util.is_notebook():
serv = context_state.get_server()
Expand Down Expand Up @@ -105,7 +105,7 @@ def get_client() -> typing.Optional[ClientInterface]:


def get_frontend_url():
from weave import server
from weave.legacy import server

url = os.environ.get("WEAVE_FRONTEND_URL", context_state.get_frontend_url())
if url is None:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from weave.server import handle_request
from weave.legacy.server import handle_request


def test_playback():
Expand Down
42 changes: 21 additions & 21 deletions weave/server.py → weave/legacy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from weave.legacy.language_features.tagging import tag_store
from weave.legacy.language_features.tagging.tag_store import isolated_tagging_context

from . import logs, storage, util, weave_types
from .. import logs, storage, util, weave_types

# A function to monkeypatch the request post method
# def patch_request_post():
Expand Down Expand Up @@ -59,14 +59,14 @@ class HandleRequestResponse:
nodes: value_or_error.ValueOrErrors[graph.Node]


def handle_request(
def handle_request( # type: ignore
request, deref=False, serialize_fn=storage.to_python
) -> HandleRequestResponse:
# Stuff in the server path relies on lazy execution. Eager is now the default
# in the weave package itself, so we need to switch to lazy mode here.
with context.lazy_execution():
start_time = time.time()
tracer = engine_trace.tracer()
tracer = engine_trace.tracer() # type: ignore[no-untyped-call]
# Need to add wandb_api.from_environment, which sets up the wandb api
# The existing code only did this within the execute() function. But now
# I'm hitting the need for this in deserialize, because node_id in deserialize
Expand All @@ -90,7 +90,7 @@ def handle_request(
result = result.zip(nodes).safe_map(
lambda t: t[0]
if isinstance(t[1].type, weave_types.RefType)
else storage.deref(t[0])
else storage.deref(t[0]) # type: ignore[no-untyped-call]
)

# print("Server request %s (%0.5fs): %s..." % (start_time,
Expand All @@ -110,12 +110,12 @@ def handle_request(


class SubprocessServer(multiprocessing.Process):
def __init__(self, req_queue, resp_queue):
def __init__(self, req_queue, resp_queue): # type: ignore
multiprocessing.Process.__init__(self)
self.req_queue = req_queue
self.resp_queue = resp_queue

def run(self):
def run(self): # type: ignore
while True:
req = self.req_queue.get()
try:
Expand All @@ -127,37 +127,37 @@ def run(self):
self.resp_queue.put(Exception("Caught exception in sub-process server"))
break

def shutdown(self):
def shutdown(self): # type: ignore
self.kill()


class SubprocessServerClient:
def __init__(self):
def __init__(self): # type: ignore
self.req_queue = multiprocessing.Queue()
self.resp_queue = multiprocessing.Queue()
self.server_proc = SubprocessServer(self.req_queue, self.resp_queue)
self.server_proc.start()

def shutdown(self):
def shutdown(self): # type: ignore
self.server_proc.shutdown()

def execute(self, nodes, no_cache=False):
def execute(self, nodes, no_cache=False): # type: ignore
self.req_queue.put({"graphs": serialize.serialize(nodes)})
response = self.resp_queue.get()
deserialized = [storage.from_python(r) for r in response]
return [storage.deref(r) for r in deserialized]


class InProcessServer(object):
def __init__(self):
def __init__(self): # type: ignore
pass

def execute(self, nodes, no_cache=False):
def execute(self, nodes, no_cache=False): # type: ignore
return execute.execute_nodes(nodes, no_cache=no_cache).unwrap()


class HttpServerClient(object):
def __init__(self, url, emulate_weavejs=False, auth: OptionalAuthType = None):
def __init__(self, url, emulate_weavejs=False, auth: OptionalAuthType = None): # type: ignore
"""Constructor.
Args:
Expand All @@ -172,7 +172,7 @@ def __init__(self, url, emulate_weavejs=False, auth: OptionalAuthType = None):
if emulate_weavejs:
self.execute_endpoint = "/__weave/execute"

def execute(self, nodes, no_cache=False):
def execute(self, nodes, no_cache=False): # type: ignore
serialized = serialize.serialize(nodes)
r = requests.post(
self.url + self.execute_endpoint,
Expand All @@ -195,8 +195,8 @@ def execute(self, nodes, no_cache=False):


class HttpServer(threading.Thread):
def __init__(self, port=0, host="localhost"):
from . import weave_server
def __init__(self, port=0, host="localhost"): # type: ignore
from weave import weave_server

self.host = host

Expand All @@ -209,10 +209,10 @@ def __init__(self, port=0, host="localhost"):
self.port = self.srv.socket.getsockname()[1]

@property
def name(self):
def name(self): # type: ignore
return f"Weave Port: {self.port}"

def run(self):
def run(self): # type: ignore
if _REQUESTED_SERVER_LOG_LEVEL is None:
capture_weave_server_logs(logging.ERROR)

Expand All @@ -227,11 +227,11 @@ def run(self):

self.srv.serve_forever()

def shutdown(self):
def shutdown(self): # type: ignore
self.srv.shutdown()

@property
def url(self):
def url(self): # type: ignore
if util.is_colab():
url = f"https://{self.host}"
else:
Expand All @@ -241,7 +241,7 @@ def url(self):
return url


def capture_weave_server_logs(log_level: int = logging.INFO):
def capture_weave_server_logs(log_level: int = logging.INFO): # type: ignore
global _REQUESTED_SERVER_LOG_LEVEL
_REQUESTED_SERVER_LOG_LEVEL = log_level

Expand Down
2 changes: 1 addition & 1 deletion weave/tests/legacy/test_execution_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from weave import storage
from weave.legacy import serialize
from weave.server import handle_request
from weave.legacy.server import handle_request


def test_graph_playback(dev_only_admin_env_override):
Expand Down
4 changes: 2 additions & 2 deletions weave/tests/legacy/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import pytest
import requests

from weave.legacy import context, ops
from weave.legacy import context, ops, server

from ... import api, logs, server, weave_server
from ... import api, logs, weave_server


def test_logfile_created(fresh_server_logfile):
Expand Down
2 changes: 1 addition & 1 deletion weave/tests/trace/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import requests

from weave import api as weave
from weave import server as _server
from weave import weave_types as types
from weave.legacy import client as _client
from weave.legacy import context_state, ops
from weave.legacy import server as _server
from weave.legacy.decorator_op import op
from weave.weave_internal import make_const_node

Expand Down
2 changes: 1 addition & 1 deletion weave/weave_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
errors,
filesystem,
logs,
server,
storage,
util,
)
Expand All @@ -39,6 +38,7 @@
engine_trace,
graph,
registry_mem,
server,
value_or_error,
wandb_api,
weavejs_fixes,
Expand Down

0 comments on commit 0339298

Please sign in to comment.