nodejs两个例子
1.nodejs多线程
var cluster=require("cluster");//ͨ¹ýcluster¿ÉÒÔ·Ö³öһЩ×ÓÏß³Ì
var http=require("http");
var numCPUs=require("os").cpus().length;//»ñµÃCPUÊǼ¸ºË
console.log(numCPUs);
var rssWarn=(12*1024*1024);
var heapWarn=(10 * 1024 * 1024);
if (cluster.isMaster) {
for (i = 0; i < numCPUs; i++) {
var worker = cluster.fork();
worker.on("message", function (m) {
if (m.memory) {
if (m.memory.rss > rssWarn) {
console.log('Worker' + m.process + " using to much memory");
}else {
console.log('Worker' + m.process + " is processing");
}
}
})
}
cluster.on("death", function (worker) {
console.log("worker " + worker.pid + ' died');
cluster.fork();
});
}else {
http.Server(function (req, res) {
res.writeHead(200);
res.end("Hello World\n");
}).listen(3000);
setInterval(function report() {
process.send({ "memory": process.memoryUsage(), "process": process.pid });
}, 1000);
}
2.nodejs日志分块
var fs=require('fs');
var log='D:/nodejs/log_file.log';//ÈÕÖ¾Îļþ·¾¶
var buf;
var childPath=__dirname;//·Ö¸îµÄÈÕÖ¾Îļþ´æ·ÅµÄ·¾¶
var buf_size=1024*10;//·Ö¸îÈÕÖ¾Îļþ×ÓÎļþ×î´óµÄ´óС
var logSize;//ÇóµÄÈÕÖ¾Îļþ´óС
var offse=0;
fs.stat(log,function(err,stats){
if(err) throw err;
logSize=stats.size;//µÃµ½Îļþ´óС
//´ò¿ªÎļþ
fs.open(log,'r',0666,function(err,fd){
if(err) throw err;
buf=new Buffer(buf_size);
readLog(fd);
});
});
var lognumber=0;
function readLog(fd){
lognumber++;
//¶ÁÈ¡ÈÕÖ¾
fs.read(fd,buf,0,buf_size,offse,function(err){
if(err) throw err;
logArr=buf.toString('utf8').split('\n');
var last=false;
if(offse+buf_size<logSize){
last=logArr.pop();
// console.log(last);
}
writeChildLog(childPath+"/logchild"+lognumber+'.log',logArr);
// console.log(offse+"|"+last.length+"|"+logSize);
if(last.length>0){
offse+=buf_size-last.length;
// console.log(offse);
buf=new Buffer(buf_size);
if(offse<logSize){
readLog(fd);
}
}
});
}
function writeChildLog(filename,logArr){
console.log(logArr.join("\n"));
fs.open(filename,'w',function(err,fd){
if(err) throw err;
fs.write(fd,logArr.join("\n"),0,'utf8',function(e){
if (e) throw e;
fs.close(fd);
});
});
}
nodejs两个例子的更多相关文章
- C#堆栈原理(我有两个例子测试你到底会不会)
背景 上次写了一篇文章关于try finnally的一些疑问(被我用windows live覆盖了,草),后来经过大神们解释,我明白了在我理解了try.finnally运行原理后,还欠缺的就是关于值类 ...
- (3两个例子)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html2环境架设http://www.cn ...
- 解决Linux终端乱码的两则例子
现象描述 我们先来说一下出现乱码的原因. 例子 先举个实际的例子,我们一般通过ssh远程到服务器上进行操作.当在终端上执行一些有输出的任务时,有可能会遇到乱码,特别是输出中有中文时. 比如,我登陆上o ...
- HTTP基础(分析两个例子)
两个例子(一个get,一个post)(一个是访问页面,一个是提交修改后的博文): preferences.aspx:(header)(文件) 1. Remote Address:42.121. ...
- tkinter 的两个例子
第一个例子:after 用于定时操作 import tkinter as tk import time class MyApp(tk.Frame): def __init__(self, msecs= ...
- 161223、mysql锁的两个例子
版本:mysql5.5.52 存储引擎:InnoDB 隔离级别:READ-COMMITTED 示例一: 事务1:左图 事务2:右图 1. 事务2中属于快照读,基于多版本的并发控制协议--MV ...
- android:layout_weight越大所占比例越大和越大所占比例越小的两个例子
摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3907146.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...
- php中使用curl两个例子
第一个例子: 调用一个天气预告的接口 $data = 'theCityName=石家庄'; $cUrl = curl_init(); curl_setopt($cUrl, CURLOPT_URL, & ...
- Android线程之Thread 、Runnable 的两个例子
Thread例子: package fy.test; import android.app.Activity; import android.os.Bundle; import android.os. ...
随机推荐
- springboot查找配置文件路径的过程
spring加载配置文件是通过listener监视器实现的,在springboot启动时: 在容器启动完成后会广播一个SpringApplicationEvent事件,而SpringApplicati ...
- 修改原型给数组对象添加forEach
Array.prototype.forEach = function (callback, context) { for (var i = 0; i < this.length; i++) { ...
- 12 个免费在线的 Web 网站性能测试工具
https://www.oschina.net/news/21033/12-free-online-tools-for-website-testing
- 使用c++为node.js扩展模块
官方文档 编写c++代码 // demo.cc #include <node.h> using v8::FunctionCallbackInfo; using v8::Isolate; u ...
- 标C编程笔记day05 函数声明、文件读写、联合类型、枚举类型
函数声明: 1.隐式声明:在没有声明的情况下,系统可依据參数类型推断去调用函数(有可能出错) 2.显式声明:声明在被调用之前.如:double add(double,double); ...
- hdu 1754 I Hate It(线段树之 单点更新+区间最值)
I Hate It Time Limit: 90 ...
- django用户认证系统——基本设置1
网站提供登录.注册等用户认证功能是一个常见的需求.因此,Django 提供了一套功能完整的.灵活的.易于拓展的用户认证系统:django.contrib.auth.在本教程中,我将向你展示 auth ...
- ios开发之 -- 调用系统定位获取当前经纬度与地理信息
ios 10定位: 在info.plist中加入: //允许在前台使用时获取GPS的描述 定位权限:Privacy - Location When In Use Usage Description / ...
- ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
今天第一次遇到Failed to start component [StandardEngine[Catalina].StandardHost[localhost].错误,并且在错误提示的后半段出现了 ...
- LightOJ - 1422 (Halloween Costumes)
题目链接:传送门 题目大意:要参加聚会,对应聚会要穿对应衣服,衣服可以套着穿,也可以脱下来,但脱下来之后不能再穿,问参加完所有聚会至少需要几件衣服? 题目思路:区间DP 一开始自己没有想出来状态转移方 ...