Skip to content

Commit

Permalink
feat(wasm): Modify wasm worker
Browse files Browse the repository at this point in the history
  • Loading branch information
fangyinc committed Nov 3, 2024
1 parent af3fd00 commit 91ac31b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 37 deletions.
10 changes: 10 additions & 0 deletions bindings/javascript/lyric-js-worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const interpreterTask = {
let stdout = [];
let stderr = [];
let evalResult;
let success = true;
let exit_code = 0;

// Save the original console.log and console.error
const originalLog = console.log;
Expand All @@ -30,6 +32,8 @@ const interpreterTask = {
console.log("After run code in interpreter Task");
} catch (error) {
console.error("Error occurred:", error.message);
success = false;
exit_code = 1;
} finally {
// Restore the original console.log and console.error
console.log = originalLog;
Expand All @@ -42,6 +46,8 @@ const interpreterTask = {
"content": "Execute script successfully",
"stdout": stdout.join('\n'),
"stderr": stderr.join('\n'),
"success": success,
"exit_code": exit_code,
"evalResult": evalResult // Return the result of the eval
}

Expand Down Expand Up @@ -70,6 +76,7 @@ const interpreterTask = {
let stderr = [];
let result;
let success = true;
let exit_code = 0;

// Save the original console.log and console.error
const originalLog = console.log;
Expand Down Expand Up @@ -122,6 +129,7 @@ const interpreterTask = {
"error": error.message
}
success = false;
exit_code = 1;
} finally {
// Restore the original console.log and console.error
console.log = originalLog;
Expand All @@ -133,6 +141,8 @@ const interpreterTask = {
"lang": "javascript",
"protocol": 1,
"content": "Execute script successfully",
"success": success,
"exit_code": exit_code,
"stdout": stdout.join('\n'),
"stderr": stderr.join('\n'),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def get_wasm_path():

class JavaScriptWasmTaskSpec(WasmTaskSpec):
def __init__(self):
super().__init__(str(get_wasm_path()), Language.JAVA_SCRIPT)
super().__init__(str(get_wasm_path()), Language.JAVASCRIPT)
53 changes: 27 additions & 26 deletions bindings/python/lyric-py-worker/src/worker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"""Python Worker Implementation Using WASI.
Import packages:
https://github.com/bytecodealliance/componentize-py/issues/91
Reduce the size of the generated wasm file:
wasm-tools strip -a
wasm-tools print python_task.wasm | wasm-tools strip -a -o python_task_stripped.wasm
"""

import sys
import json
import asyncio
Expand All @@ -18,42 +28,32 @@
from lyric_py_task import exports, imports
from lyric_py_task.imports import msgpack


count = 0

"""
Import packages:
https://github.com/bytecodealliance/componentize-py/issues/91
Reduce the size of the generated wasm file:
wasm-tools strip -a
wasm-tools print python_task.wasm | wasm-tools strip -a -o python_task_stripped.wasm
"""


class InterpreterTask(exports.InterpreterTask):
def run(
self, script: exports.types.InterpreterRequest
) -> exports.types.InterpreterResponse:
"""
Raises: `interpreter_task.types.Err(interpreter_task.imports.str)`
"""
global count
print(f"[Python-InterpreterTask] script: {script}")
print("[Python-InterpreterTask] count:", count)
count += 1
success = False
with IOCapture() as capture:
exec(script.code)
try:
exec(script.code)
success = True
except Exception as e:
print(f"[Python-InterpreterTask] Exception: {e}")
success = False
stdout, stderr = capture.get_output()

result_dict = {
"lang": "Python",
"protocol": 1,
"content": "Execute script successfully",
"success": success,
"exit_code": 0 if success else 1,
"stdout": stdout,
"stderr": stderr,
"success": True,
}
serialized = msgpack.serialize(
json.dumps(result_dict, ensure_ascii=False).encode("utf-8")
Expand All @@ -68,22 +68,22 @@ def run1(
print(f"[Python-InterpreterTask] script: {request}")
print(f"[Python-InterpreterTask] call_name: {call_name}")

# 执行用户脚本
# Execute the user script
exec_globals = {}
exec(request.code, exec_globals)

# 检查call_name是否在执行后的全局变量中
# Check if the function is defined in the script
if call_name not in exec_globals:
raise ValueError(f"函数 {call_name} 未在脚本中定义")
raise ValueError(f"Function {call_name} is not defined in the script")

# 获取call_name对应的函数
# Get the target function
target_function = exec_globals[call_name]

# 解码输入
# Deserialize the input
input_json = msgpack.deserialize(input).decode("utf-8")
input_dict = json.loads(input_json)

# 执行函数并捕获输出
# Execute the function and capture the output
with IOCapture() as capture:
try:
output = target_function(input_dict)
Expand All @@ -101,6 +101,7 @@ def run1(
"protocol": 1,
"content": "Execute script successfully",
"success": success,
"exit_code": 0 if success else 1,
"stdout": stdout,
"stderr": stderr,
}
Expand Down Expand Up @@ -143,7 +144,7 @@ def loads(data):


def dumps(data):
from my_py_lyric.pickle import dumps
from lyric_task.pickle import dumps

# return pickle.dumps(data)
return dumps(data)
16 changes: 8 additions & 8 deletions bindings/python/lyric-py/python/lyric/py_lyric.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ def _gen_task_id() -> str:


class Lyric:
def __init__(self, pl: PyLyric):
def __init__(self, pl: PyLyric, default_local_env: Optional[PyLocalEnvironmentConfig] = None):
from lyric import BASE_LYRIC_DIR

self._pl = pl
self._hm = HandleManager(pl)
self._default_local_env = PyLocalEnvironmentConfig(
envs={"LYRIC_CORE_LOG_ANSICOLOR": "false"}
self._default_local_env = default_local_env or PyLocalEnvironmentConfig(
envs={"LYRIC_CORE_LOG_ANSICOLOR": "false", "LYRIC_CORE_LOG_LEVEL": "ERROR"}
)
self._default_docker_env = PyDockerEnvironmentConfig(
image="py-lyric-base-alpine:latest", mounts=[(BASE_LYRIC_DIR, "/app")]
Expand All @@ -56,7 +56,7 @@ def start_driver(self):
self._pl.start_driver(PyDriverConfig())

async def load_workers(self, languages: List[LanguageType] = None):
languages = languages or [Language.PYTHON, Language.JAVA_SCRIPT]
languages = languages or [Language.PYTHON, Language.JAVASCRIPT]
languages = [Language.parse(lang) for lang in languages]
if Language.PYTHON in languages:
await self._hm.get_handler(
Expand All @@ -65,11 +65,11 @@ async def load_workers(self, languages: List[LanguageType] = None):
self._default_env(Language.PYTHON),
ignore_load_error=True,
)
if Language.JAVA_SCRIPT in languages:
if Language.JAVASCRIPT in languages:
await self._hm.get_handler(
Language.JAVA_SCRIPT,
Language.JAVASCRIPT,
_load_javascript_worker,
self._default_env(Language.JAVA_SCRIPT),
self._default_env(Language.JAVASCRIPT),
ignore_load_error=True,
)

Expand All @@ -79,7 +79,7 @@ def _default_env(self, language: LanguageType) -> PyEnvironmentConfig:
return PyEnvironmentConfig(local=self._default_local_env).clone_new(
"python_worker"
)
elif language == Language.JAVA_SCRIPT:
elif language == Language.JAVASCRIPT:
return PyEnvironmentConfig(local=self._default_local_env).clone_new(
"javascript_worker"
)
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/lyric-task/src/lyric_task/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def parse(cls: Type[T], value: Union[T, int, str, None]) -> Optional[T]:

class Language(BaseEnum):
PYTHON = 0
JAVA_SCRIPT = 1
JAVASCRIPT = 1
SHELL = 2
RUST = 3

Expand Down
2 changes: 1 addition & 1 deletion crates/lyric-rpc/proto/task.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package task;
/// Language of the code
enum Language {
PYTHON = 0;
JAVA_SCRIPT = 1;
JAVASCRIPT = 1;
SHELL = 2;
RUST = 3;
}
Expand Down

0 comments on commit 91ac31b

Please sign in to comment.