简体   繁体   English

如何在主线程和 Web Worker 之间共享 pouchDB

[英]How to share a pouchDB between main thread and a web worker

In a web app, using pouchDB , I have a slow running function that finishes by updating a document in the DB.在 Web 应用程序中,使用pouchDB ,我有一个运行缓慢的函数,它通过更新数据库中的文档来完成。 I want to move it off the main UI thread and into a web worker.我想将它从主 UI 线程移到网络工作者中。 However, we have lots of other code using pouchDB still in the main thread (eg the change event listener, but also code that deals with other documents).但是,我们还有很多其他代码使用 pouchDB 仍然在主线程中(例如change事件侦听器,还有处理其他文档的代码)。 (For reference the database size is on the order of 100MB; Vue2 is used so, in general, the UI can update when the data changes.) (作为参考,数据库大小在 100MB 左右;使用 Vue2,所以一般情况下,UI 可以在数据更改时更新。)

This is where I seem to come unstuck immediately:这就是我似乎立即解脱的地方:

  • Shared memory is basically out, as all the browsers disable it by default共享内存基本用完了,因为所有浏览器默认禁用它
  • Even if it wasn't, pouchDB is a class, and cannot be transferred(?).即使不是,pouchDB 也是一个类,不能转移(?)。
  • Isolating all the db code, including the changes handler, into one web worker is a huge refactor;将所有数据库代码(包括更改处理程序)隔离到一个 Web Worker 中是一个巨大的重构; and then we still have the issue of having to pass huge chunks of data in and out of that web worker.然后我们仍然面临必须将大量数据传入和传出该 Web Worker 的问题。
  • Move all the code that uses the data into the web worker too, and just have the UI thread pass messages back and forth, is an even bigger refactor, and I've not thought through how it might interfere with Vue.将所有使用数据的代码也移到 Web Worker 中,让 UI 线程来回传递消息,这是一个更大的重构,我还没有想过它会如何干扰 Vue。

That seems to leave us with a choice of two extremes.这似乎让我们有两个极端的选择。 Either rewrite the whole app from the ground up, possibly dropping Vue, or just do the slow, complex calculation in a web worker, then have it pass back the result, and continue to do the db.put() in the main UI thread.要么从头开始重写整个应用程序,可能会放弃 Vue,或者只是在 web worker 中进行缓慢而复杂的计算,然后让它传回结果,并继续在主 UI 线程中执行db.put() .

Is it really an all or nothing situation?这真的是全有或全无的情况吗? Are there any PouchDB "tricks" that allow working with web workers, and if so will we need to implement locking?是否有任何 PouchDB“技巧”​​允许与网络工作者一起工作,如果是这样,我们是否需要实现锁定?

You're missing an option, that I would chose in your situation.你错过了一个选项,我会在你的情况下选择。 Write a simple adapter that allows your worker code to query the DB in main thread via messages.编写一个简单的适配器,允许您的工作代码通过消息查询主线程中的数据库。 Get your data, process it in the worker and send it back.获取您的数据,在工作人员中对其进行处理并将其发回。

You only need to "wrap" the methods that you need in the worker.您只需要在工作器中“包装”您需要的方法。 I recommend writing a class or set of functions that are async in your worker, to make the code readable.我建议在您的工作线程中编写一个类或一组异步函数,以使代码可读。

You don't need to worry about the amount of data passed.您无需担心传递的数据量。 The serialization and de-serialization is quite fast and the transfer is basically memcpy , so that does not take any reasonable time.序列化和反序列化非常快,传输基本上是memcpy ,因此不需要任何合理的时间。

I found this adapter plugin, which I guess counts as the "PouchDB trick" I was after: https://github.com/pouchdb-community/worker-pouch我找到了这个适配器插件,我想这算是我追求的“PouchDB 技巧”: https : //github.com/pouchdb-community/worker-pouch

It was trivial to add (see below), and has been used in production for 6-7 weeks, and appears to have fixed the problems we saw.添加是微不足道的(见下文),已经在生产中使用了 6-7 周,似乎已经解决了我们看到的问题。 (I say appears , as it is quite hard to see it having any effect, and we didn't have a good way to reproduce the slowdown problems users were seeing.) (我说出现,因为很难看到它有任何影响,而且我们没有一个很好的方法来重现用户看到的减速问题。)

const PouchDB = require('pouchdb-browser').default
const pouchdbWorker = require('worker-pouch')
PouchDB.adapter('worker', pouchdbWorker)

The real code is like this, but usePouchDBWorker has always been kept as true :真正的代码是这样的,但是usePouchDBWorker一直保持为true

const PouchDB = require('pouchdb-browser').default
// const pouchdbDebug = require('pouchdb-debug')
if (usePouchDBWorker) {
  const pouchdbWorker = require('worker-pouch')
  PouchDB.adapter('worker', pouchdbWorker)
}

This code is used in both web app and Electron builds.此代码用于 Web 应用程序和 Electron 构建。 The web app is never used with older web browsers, so read the github site if that might be a concern for your own use case.该 Web 应用程序从未与较旧的 Web 浏览器一起使用,因此如果您自己的用例可能会担心,请阅读 github 站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM