GithubHelp home page GithubHelp logo

Comments (5)

milesj avatar milesj commented on May 9, 2024 1

Closing this since it works now in 0.4!

from extism.

nilslice avatar nilslice commented on May 9, 2024

This might be relevant: https://users.rust-lang.org/t/solved-how-to-move-non-send-between-threads-or-an-alternative/19928/7

Sorry, only have time for a quick review but thought that link might be helpful - can dig in more soon. But plugins have their own mutable memory used to persist plugin data between function calls. See https://extism.org/docs/write-a-plugin/rust-pdk/#using-config-io--persisted-variables

Can you create a new plugin context & plugin per spawned task / thread?

from extism.

jcswart avatar jcswart commented on May 9, 2024

I have been experimenting with extism and tokio.

The first such experiment, in an axum program was the following. NOTE: This is all experimental code. Its just meant to provide a slightly more concrete example than simply saying, "use a once_cell". https://docs.rs/once_cell/latest/once_cell/

struct PluginManager {}

impl PluginManager {
    fn run(&self, input: String) -> String {
        let wasm = std::fs::read("src/code.wasm").unwrap();
        let context = Context::new();
        let mut plugin = Plugin::new(&context, wasm, vec![], true).unwrap();

        let data = plugin.call("count_vowels", &input).unwrap();

        String::from_utf8(data.to_vec()).unwrap_or("error".to_string())
    }
}

Then in main.rs:

static PLUGIN_MANAGER: OnceCell<PluginManager> = OnceCell::new();

And before I start the axum server:

let _ = PLUGIN_MANAGER.set(PluginManager {});

Now in any route I can simply:

let plugins = PLUGIN_MANAGER.get().expect("failed to get plugin manager, was it set?");
let result = plugins.run("input");

The next experiment is to update the PluginManager to separate plugin compilation from execution. If there isn't an obvious way to do this with the once_cell alone I will likely incorporate a https://github.com/xacrimon/dashmap as they allow you to safely update across threads.

from extism.

milesj avatar milesj commented on May 9, 2024

Yeah I was thinking of using a similar approach. Basically have a plugin registry singleton using OnceCell.

from extism.

bhelx avatar bhelx commented on May 9, 2024

Thanks for following up!

from extism.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.