Neon: How do I share variables in multiple node process?

Created on 24 Feb 2020  路  7Comments  路  Source: neon-bindings/neon

  • I want to write an ffi to implement the help node cache

  • However, I found that I had a problem using pm2 to start multiple nodejs processes

    I found that I could not share this hashmap

  • ## my error

// start
pm2 start lib/index.js -i 2 --name cache

// I want the effect
4|cache | start
4|cache |
4|cache | undefined
4|cache | hello world
4|cache | end

5|cache | start
5|cache | hello world // !!!!!!!!!!! right
5|cache | undefined
5|cache | hello world
5|cache | end

// But actually
4|cache | start
4|cache |
4|cache | undefined
4|cache | hello world
4|cache | end

5|cache | start
5|cache | // !!!!!!!!!!! error
5|cache | undefined
5|cache | hello world
5|cache | end
`

  • ## this is my code
    Rust
#[macro_use]
extern crate lazy_static;

extern crate neon;

use neon::prelude::*;
use neon::register_module;
use std::collections::HashMap;
use std::sync::Mutex;

lazy_static! {
  static ref HASHMAP: Mutex<HashMap<String, String>> = Mutex::new(HashMap::new());
}

fn hashmap_put(key: String, value: String) {
  HASHMAP.lock().unwrap().insert(key, value);
}

fn hashmap_remove(key: String) {
  HASHMAP.lock().unwrap().remove(&key);
}

fn hashmap_get(key: String) -> String {
  match HASHMAP.lock().unwrap().get(&key) {
    Some(value) => value.to_owned(),
    None => "".to_owned(),
  }
}

fn put(mut cx: FunctionContext) -> JsResult<JsUndefined> {
  let key = cx.argument::<JsString>(0)?.value();
  let value = cx.argument::<JsString>(1)?.value();
  hashmap_put(key, value);
  Ok(cx.undefined())
}

fn remove(mut cx: FunctionContext) -> JsResult<JsUndefined> {
  let key = cx.argument::<JsString>(0)?.value();
  hashmap_remove(key);
  Ok(cx.undefined())
}

fn get(mut cx: FunctionContext) -> JsResult<JsString> {
  let key = cx.argument::<JsString>(0)?.value();
  Ok(cx.string(hashmap_get(key)))
}

register_module!(mut m, {
  m.export_function("put", put)?;
  m.export_function("remove", remove)?;
  m.export_function("get", get)?;
  Ok(())
});

nodeJS

var addon = require('../native');
console.log('start')

console.log(addon.get('1')); // exist?

console.log(addon.put('1',"hello world"));

console.log(addon.get('1'));

console.log('end')
  • ## What should I do锛焢lease give some advice

Most helpful comment

The difficult factor here is that you build on top of PM2, as @Brooooooklyn already mentioned you could use wrapper code to manage the memory for you, use IPC or a different alternative to this is to let node manage the memory for you by utilizing worker threads. By doing this they share the same Process and Isolate.

All 7 comments

you start multiple nodejs processes, which means you have multiple HASHMAP instance

you start multiple nodejs processes, which means you have multiple HASHMAP instance

What do I need to do to complete multiple node processes one HASHMAP instance,

Thank you for your advice

What do I need to do to complete multiple node processes one HASHMAP instance

Perhaps you can manage node process in the Rust side, so that you can share memory to which node processes you forked.
But this solution too complex to implement. Maybe you should consider share cache with Redis or something similar.

I don't really want to use redis, because what I want is very simple. Let me think again

The difficult factor here is that you build on top of PM2, as @Brooooooklyn already mentioned you could use wrapper code to manage the memory for you, use IPC or a different alternative to this is to let node manage the memory for you by utilizing worker threads. By doing this they share the same Process and Isolate.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dherman picture dherman  路  4Comments

vasishath picture vasishath  路  3Comments

thehappycoder picture thehappycoder  路  3Comments

matklad picture matklad  路  5Comments

ZhangHanDong picture ZhangHanDong  路  5Comments