mirror of
https://github.com/extism/extism.git
synced 2026-04-23 03:00:11 -04:00
This PR adds the ability to cancel running plugins from another thread in the host. - All SDKs have been updated except .NET - Adds a new SDK type: `ExtismCancelHandle` - Adds 2 new SDK functions: `extism_plugin_cancel_handle` and `extism_plugin_cancel` - `extism_plugin_cancel_handle` returns a pointer to `ExtismCancelHandle`, which can be passed to `extism_plugin_cancel` to stop plugin execution. - One thing that's worth noting is when plugin is executing a host function it cannot be cancelled until after the host function returns. --------- Co-authored-by: Etienne ANNE <etienne.anne@icloud.com>
62 lines
1.6 KiB
Python
62 lines
1.6 KiB
Python
import sys
|
|
|
|
import json
|
|
import hashlib
|
|
import pathlib
|
|
|
|
sys.path.append(".")
|
|
from extism import Context, Function, host_fn, ValType
|
|
|
|
|
|
@host_fn
|
|
def hello_world(plugin, input_, output, context, a_string):
|
|
print("Hello from Python!")
|
|
print(a_string)
|
|
print(input_)
|
|
print(plugin.input_string(input_[0]))
|
|
output[0] = input_[0]
|
|
|
|
|
|
# Compare against Python implementation.
|
|
def count_vowels(data):
|
|
return sum(letter in b"AaEeIiOoUu" for letter in data)
|
|
|
|
|
|
def main(args):
|
|
if len(args) > 1:
|
|
data = args[1].encode()
|
|
else:
|
|
data = b"some data from python!"
|
|
|
|
wasm_file_path = (
|
|
pathlib.Path(__file__).parent.parent / "wasm" / "code-functions.wasm"
|
|
)
|
|
wasm = wasm_file_path.read_bytes()
|
|
hash = hashlib.sha256(wasm).hexdigest()
|
|
config = {"wasm": [{"data": wasm, "hash": hash}], "memory": {"max": 5}}
|
|
|
|
# a Context provides a scope for plugins to be managed within. creating multiple contexts
|
|
# is expected and groups plugins based on source/tenant/lifetime etc.
|
|
with Context() as context:
|
|
functions = [
|
|
Function(
|
|
"hello_world",
|
|
[ValType.I64],
|
|
[ValType.I64],
|
|
hello_world,
|
|
context,
|
|
"Hello again!",
|
|
)
|
|
]
|
|
plugin = context.plugin(config, wasi=True, functions=functions)
|
|
# Call `count_vowels`
|
|
wasm_vowel_count = json.loads(plugin.call("count_vowels", data))
|
|
|
|
print("Number of vowels:", wasm_vowel_count["count"])
|
|
|
|
assert wasm_vowel_count["count"] == count_vowels(data)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(sys.argv)
|