js的server worker创建子进程
类似nodejs的 child_process.fork()
// index.html 主线程
function isClose(data){
if(data === 0)
return true;
}
var myWorker = new Worker("worker.js");
myWorker.addEventListener('error',err=>{
console.log( err);
}, false);
myWorker.addEventListener('message',msg=>{
// 监听子线程发来的 数据
console.log( msg.data);
if( isClose(msg.data) ){
// 干掉子进程
myWorker.terminate();
}
}, false);
setTimeout(function(){
// 发更子进程的数据
myWorker.postMessage('frok')
}, 2000);
// worker.js 子线程
// 加载 fn.js 文件
importScripts('fn.js');
console.log(123);
// 向主线程发送 data
postMessage('over 123');
// 接收主线程发来的的数据
onmessage = (e)=>{
check(e.data);
console.log( e.data);
}
// fn.js
function check(data){
console.log( data.constructor.name );
}
动态创建server.js文件
const workerJSString = `
// 接收主线程发来的的数据
onmessage = ({data})=>{
// 向主线程发送data
console.log(\`接收到主线程发来的数据: \${data}\`);
postMessage(data ** 2);
}
`;
const workerBlob = new Blob([workerJSString], {
type: "text/javascript"
});
const url = URL.createObjectURL(workerBlob);
const myWorker = new Worker(url);
// 监听子线程发来的 数据
myWorker.addEventListener("message", ({ data }) => {
console.log(data); // 9
});
setTimeout(function () {
// 向子进程的数据
myWorker.postMessage(3);
}, 2000);
js的server worker创建子进程的更多相关文章
- swoole之创建子进程
一.代码 <?php /** * 进程就是正在运行的程序的一个实例 * 比如,在某个终端中执行一个PHP脚本,可以认为就是开启了一个进程,会有对应的进程id(pid) * * swoole进程与 ...
- Nginx学习笔记(七) 创建子进程
Nginx创建子进程 ngx_start_worker_processes位于Nginx_process_cycle.c中,主要的工作是创建子进程. 在Nginx中,master进程和worker进程 ...
- node.js中express模块创建服务器和http模块客户端发请求
首先下载express模块,命令行输入 npm install express 1.node.js中express模块创建服务端 在js代码同文件位置新建一个文件夹(www_root),里面存放网页文 ...
- node.js中ws模块创建服务端和客户端,网页WebSocket客户端
首先下载websocket模块,命令行输入 npm install ws 1.node.js中ws模块创建服务端 // 加载node上websocket模块 ws; var ws = require( ...
- node.js中net模块创建服务器和客户端(TCP)
node.js中net模块创建服务器和客户端 1.node.js中net模块创建服务器(net.createServer) // 将net模块 引入进来 var net = require(" ...
- nova-api源码分析(WSGI server的创建及启动)
源码版本:H版 一.前奏 nova api本身作为一个WSGI服务器,对外提供HTTP请求服务,对内调用nova的其他模块响应相应的HTTP请求.分为两大部分,一是服务器本身的启动与运行,一是加载的a ...
- fork()创建子进程
fork()系统调用是Unix下以自身进程创建子进程的系统调用,一次调用,两次返回,如果返回是0,则是子进程,如果返回值>0,则是父进程(返回值是子进程的pid) 在fork()的调用处,整个父 ...
- 在64位SQL Server中创建Oracle的链接服务器
当我们同时使用SQL Server和Oracle来存储数据时,经常会用到跨库查询.为了方便使用跨库查询,一个最好的办法就是通过创建链接服务器来实现.既可以在SQL Server中创建Oracle的链接 ...
- sql server 脚本创建数据库邮件
sql server 脚本创建数据库邮件代码: --脚本创建数据库邮件 --1.开启数据库邮件 RECONFIGURE WITH OVERRIDE GO RECONFIGURE WITH OVERRI ...
随机推荐
- gitolite migration to bitbucket
https://gist.github.com/kostajh/9249937 https://designhammer.com/blog/easily-migrate-git-repositorie ...
- Android性能优化-减小APK大小
前言 用户通常会避免下载比较大的应用,特别是连接到2G和3G网络,或者按流量收费的设备.这篇文章描述了如何减小apk的大小,帮助你让更多的用户下载你的app. 一 理解APK的结构 在讨论如何减小ap ...
- RHEL磁盘修复
0. 1.基础工具:e2label /device/xxx [new label name] 显示/设定设备的label名称 2.e2fsck 修复工具,用-b 指定备用的superblock位置 ...
- AutoMapperExtension
using System; using System.Collections.Generic; using System.Linq; using AutoMapper; using System.Co ...
- redis 基本信息查询
在客户端可以用telnet命令 telnet ip port 再输入info 返回如下信息:
- PHP测试Mysql数据库连接
<?php $link = mysqli_connect('localhost', 'username', 'password'); if (!$link) { die('Could not c ...
- 9.4 翻译系列:EF 6以及 EF Core中的NotMapped特性(EF 6 Code-First系列)
原文链接:http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-f ...
- Zabbix-2.X/3.X监控工具监控Redis以及zabbix Redis监控模板下载
为了监控Redis3的运行状况,去zabbix官网查找资料,根据提示,找到了这个项目:https://github.com/blacked/zbx_redis_template 但是文档和内容已经不匹 ...
- 在Vue项目中使用vw实现移动端适配
有关于移动端的适配布局一直以来都是众说纷纭,对应的解决方案也是有很多种.在<使用Flexible实现手淘H5页面的终端适配>提出了Flexible的布局方案,随着viewport单位越来越 ...
- lua -- 所有UI组件的基类
-- 组件行为基础 local Behavior = class("Behavior"); function Behavior:ctor(name) self.owner = ni ...