日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html worker api,worker_threads 工作线程

發布時間:2023/12/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html worker api,worker_threads 工作线程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

MessagePort 類

版本歷史

版本變更

v14.7.0

This class now inherits from EventTarget rather than from EventEmitter.

v10.5.0

新增于: v10.5.0

Instances of the worker.MessagePort class represent one end of an

asynchronous, two-way communications channel. It can be used to transfer

structured data, memory regions and other MessagePorts between different

Workers.

With the exception of MessagePorts being [EventEmitter][]s rather

than EventTargets, this implementation matches browser MessagePorts.

'close' 事件

新增于: v10.5.0

The 'close' event is emitted once either side of the channel has been

disconnected.

const { MessageChannel } = require('worker_threads');

const { port1, port2 } = new MessageChannel();

// Prints:

// foobar

// closed!

port2.on('message', (message) => console.log(message));

port2.on('close', () => console.log('closed!'));

port1.postMessage('foobar');

port1.close();

'message' 事件

新增于: v10.5.0

value The transmitted value

The 'message' event is emitted for any incoming message, containing the cloned

input of port.postMessage().

Listeners on this event receive a clone of the value parameter as passed

to postMessage() and no further arguments.

'messageerror' 事件

新增于: v14.5.0, v12.19.0

error An Error object

The 'messageerror' event is emitted when deserializing a message failed.

Currently, this event is emitted when there is an error occurring while

instantiating the posted JS object on the receiving end. Such situations

are rare, but can happen, for instance, when certain Node.js API objects

are received in a vm.Context (where Node.js APIs are currently

unavailable).

port.close()

新增于: v10.5.0

Disables further sending of messages on either side of the connection.

This method can be called when no further communication will happen over this

MessagePort.

The 'close' event is emitted on both MessagePort instances that

are part of the channel.

port.postMessage(value[, transferList])

版本歷史

版本變更

v15.14.0

Add 'BlockList' to the list of cloneable types.

v15.9.0

Add 'Histogram' types to the list of cloneable types.

v15.6.0

Added X509Certificate to the list of cloneable types.

v15.0.0

Added CryptoKey to the list of cloneable types.

v14.5.0, v12.19.0

Added KeyObject to the list of cloneable types.

v14.5.0, v12.19.0

Added FileHandle to the list of transferable types.

v10.5.0

新增于: v10.5.0

value

transferList

Sends a JavaScript value to the receiving side of this channel.

value is transferred in a way which is compatible with

the HTML structured clone algorithm.

In particular, the significant differences to JSON are:

value may contain circular references.

value may contain instances of builtin JS types such as RegExps,

BigInts, Maps, Sets, etc.

value may contain typed arrays, both using ArrayBuffers

and SharedArrayBuffers.

value may contain WebAssembly.Module instances.

const { MessageChannel } = require('worker_threads');

const { port1, port2 } = new MessageChannel();

port1.on('message', (message) => console.log(message));

const circularData = {};

circularData.foo = circularData;

// Prints: { foo: [Circular] }

port2.postMessage(circularData);

transferList may be a list of ArrayBuffer, MessagePort and

FileHandle objects.

After transferring, they are not usable on the sending side of the channel

anymore (even if they are not contained in value). Unlike with

child processes, transferring handles such as network sockets is currently

not supported.

If value contains SharedArrayBuffer instances, those are accessible

from either thread. They cannot be listed in transferList.

value may still contain ArrayBuffer instances that are not in

transferList; in that case, the underlying memory is copied rather than moved.

const { MessageChannel } = require('worker_threads');

const { port1, port2 } = new MessageChannel();

port1.on('message', (message) => console.log(message));

const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);

// This posts a copy of `uint8Array`:

port2.postMessage(uint8Array);

// This does not copy data, but renders `uint8Array` unusable:

port2.postMessage(uint8Array, [ uint8Array.buffer ]);

// The memory for the `sharedUint8Array` is accessible from both the

// original and the copy received by `.on('message')`:

const sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4));

port2.postMessage(sharedUint8Array);

// This transfers a freshly created message port to the receiver.

// This can be used, for example, to create communication channels between

// multiple `Worker` threads that are children of the same parent thread.

const otherChannel = new MessageChannel();

port2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]);

The message object is cloned immediately, and can be modified after

posting without having side effects.

For more information on the serialization and deserialization mechanisms

behind this API, see the serialization API of the v8 module.

傳輸 TypedArray 和 Buffer 時的注意事項

All TypedArray and Buffer instances are views over an underlying

ArrayBuffer. That is, it is the ArrayBuffer that actually stores

the raw data while the TypedArray and Buffer objects provide a

way of viewing and manipulating the data. It is possible and common

for multiple views to be created over the same ArrayBuffer instance.

Great care must be taken when using a transfer list to transfer an

ArrayBuffer as doing so causes all TypedArray and Buffer

instances that share that same ArrayBuffer to become unusable.

const ab = new ArrayBuffer(10);

const u1 = new Uint8Array(ab);

const u2 = new Uint16Array(ab);

console.log(u2.length); // prints 5

port.postMessage(u1, [u1.buffer]);

console.log(u2.length); // prints 0

For Buffer instances, specifically, whether the underlying

ArrayBuffer can be transferred or cloned depends entirely on how

instances were created, which often cannot be reliably determined.

An ArrayBuffer can be marked with markAsUntransferable() to indicate

that it should always be cloned and never transferred.

Depending on how a Buffer instance was created, it may or may

not own its underlying ArrayBuffer. An ArrayBuffer must not

be transferred unless it is known that the Buffer instance

owns it. In particular, for Buffers created from the internal

Buffer pool (using, for instance Buffer.from() or Buffer.allocUnsafe()),

transferring them is not possible and they are always cloned,

which sends a copy of the entire Buffer pool.

This behavior may come with unintended higher memory

usage and possible security concerns.

See Buffer.allocUnsafe() for more details on Buffer pooling.

The ArrayBuffers for Buffer instances created using

Buffer.alloc() or Buffer.allocUnsafeSlow() can always be

transferred but doing so renders all other existing views of

those ArrayBuffers unusable.

使用原型、類和訪問器克隆對象時的注意事項

Because object cloning uses the HTML structured clone algorithm,

non-enumerable properties, property accessors, and object prototypes are

not preserved. In particular, Buffer objects will be read as

plain Uint8Arrays on the receiving side, and instances of JavaScript

classes will be cloned as plain JavaScript objects.

const b = Symbol('b');

class Foo{

#a = 1;

constructor() {

this[b] = 2;

this.c = 3;

}

get d() { return 4; }

}

const { port1, port2 } = new MessageChannel();

port1.onmessage = ({ data }) => console.log(data);

port2.postMessage(new Foo());

// Prints: { c: 3 }

This limitation extends to many built-in objects, such as the global URL

object:

const { port1, port2 } = new MessageChannel();

port1.onmessage = ({ data }) => console.log(data);

port2.postMessage(new URL('https://example.org'));

// Prints: { }

port.ref()

新增于: v10.5.0

Opposite of unref(). Calling ref() on a previously unref()ed port does

not let the program exit if it's the only active handle left (the default

behavior). If the port is ref()ed, calling ref() again has no effect.

If listeners are attached or removed using .on('message'), the port

is ref()ed and unref()ed automatically depending on whether

listeners for the event exist.

port.start()

新增于: v10.5.0

Starts receiving messages on this MessagePort. When using this port

as an event emitter, this is called automatically once 'message'

listeners are attached.

This method exists for parity with the Web MessagePort API. In Node.js,

it is only useful for ignoring messages when no event listener is present.

Node.js also diverges in its handling of .onmessage. Setting it

automatically calls .start(), but unsetting it lets messages queue up

until a new handler is set or the port is discarded.

port.unref()

新增于: v10.5.0

Calling unref() on a port allows the thread to exit if this is the only

active handle in the event system. If the port is already unref()ed calling

unref() again has no effect.

If listeners are attached or removed using .on('message'), the port is

ref()ed and unref()ed automatically depending on whether

listeners for the event exist.

總結

以上是生活随笔為你收集整理的html worker api,worker_threads 工作线程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。