Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sqlite3 in web worker doesn't work #34

Closed
Piero87 opened this issue Sep 5, 2014 · 3 comments
Closed

Use sqlite3 in web worker doesn't work #34

Piero87 opened this issue Sep 5, 2014 · 3 comments

Comments

@Piero87
Copy link

Piero87 commented Sep 5, 2014

hi,
i'm trying to access to a sqlite3 db inside the web worker, i use this sqlite3 library:

https://github.com/mapbox/node-sqlite3

this is what i do:

var Worker = require('webworker-threads').Worker;
var worker = new Worker(function(){
  postMessage("I'm working before postMessage('ali').");
  this.onmessage = function(event) {
    console.log("before sqlite3");
    var sqlite3 = require('sqlite3').verbose();
    var db = new sqlite3.Database('MyDB.db');
    console.log("after sqlite3");
  };
});

worker.postMessage('test');

this is the result:

before sqlite3

and then block, how i can solve the problem? i do something wrong?

@audreyt
Copy link
Owner

audreyt commented Sep 5, 2014

Hi, currently we do not support require() in threads, there is only importScripts, which only supports pure-JavaScript modules (via browserify, webpack, onejs etc), and it cannot write to the filesystem.

Perhaps you could move the SQLite-access functions to the main thread, and postMessage from within the worker to access it?

@audreyt audreyt closed this as completed Sep 5, 2014
@Piero87
Copy link
Author

Piero87 commented Sep 5, 2014

so you mean pass this: var db, to the worker?

@audreyt
Copy link
Owner

audreyt commented Sep 5, 2014

I mean something like:

var Worker = require('webworker-threads').Worker;
var sqlite3 = require('sqlite3').verbose();
var worker = new Worker(function(){
  this.postMessage(...) // query to db
  this.onmessage = function(event) {
      // ...handler result from SQLite3...
  };
});

var db = new sqlite3.Database('MyDB.db');
worker.onmessage = function (event) {
   // ... do something with db ...
}

Basically doing all DB filesystem operations in the main threads, and have the worker do CPU-intensive work. Something like https://github.com/mozilla/jschannel might be useful to make postMessage easier to reason with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants