Skip to content

Instantly share code, notes, and snippets.

@lqqyt2423
lqqyt2423 / .js
Created February 26, 2019 12:13
node master worker
// master.js
'use strict';
const fork = require('child_process').fork;
const cpus = require('os').cpus();
console.log('master pid:', process.pid);
for (let i = 0; i < cpus.length; i++) {
@lqqyt2423
lqqyt2423 / udp.js
Created February 26, 2019 06:46
node udp 示例
// server.js
'use strict';
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('error', err => {
console.log(`server error:\n${err.stack}`);
server.close();
@lqqyt2423
lqqyt2423 / tcp.js
Created February 26, 2019 06:29
node tcp 示例
// server.js
'use strict';
const net = require('net');
const server = net.createServer(function (socket) {
socket.setEncoding('utf-8');
// 新的连接
socket.on('data', function (data) {
@lqqyt2423
lqqyt2423 / node-heapdump.js
Created February 26, 2019 02:45
内存快照,通过chrome开发者工具查看内存快照
'use strict';
// kill -USR2 61365
const heapdump = require('heapdump');
const http = require('http');
const leakArray = [];
const leak = function () {
leakArray.push('leak' + Math.random());
@lqqyt2423
lqqyt2423 / memory.js
Created February 25, 2019 10:20
node 内存展示
'use strict';
const os = require('os');
function format (bytes) {
return (bytes / 1024 / 1024).toFixed(2) + ' MB';
}
function showMem () {
const mem = process.memoryUsage();
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/", handler)
'use strict';
const http = require('http');
const server = http.createServer((req, res) => {
console.log(req.url);
res.end('simple server');
});
server.on('error', err => {
function fibonacci(n) {
if (n == 0 || n == 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
const now = Date.now();
const i = fibonacci(40);
console.log(i);
console.log(String(Date.now() - now), 'ms');
@lqqyt2423
lqqyt2423 / list_all_code.py
Created February 25, 2019 06:19
合并当前目录下的文件,全部打印出来
#! /usr/bin/env python3
import os
root_dir = os.getcwd()
names = ["js"]
def list_code(dir):
for lists in os.listdir(dir):
require "fileutils"
# 循环文件夹的块包装
def each_dir(dirname)
Dir.open(dirname) do |dir|
dir.each do |name|
next if name == "."
next if name == ".."
yield name
end