Skip to content

Instantly share code, notes, and snippets.

View lightyears1998's full-sized avatar

lightyears lightyears1998

View GitHub Profile
@lightyears1998
lightyears1998 / caching-decorator.py
Last active November 28, 2021 03:59
Python caching with decorator
import random
from functools import wraps
disk = dict()
def read_cache(path):
return disk.get(path, None)
@lightyears1998
lightyears1998 / NOTE.md
Created August 28, 2020 06:49
IPC over node.js (with serialization: 'advanced')

如果 ClusterSettingsserialization 键设为 "json" 或不设置,则 Error 的内容是不能正常传递的,只能得到一个空对象。

默认设置下,IPC 可能适合传递小数据。

@lightyears1998
lightyears1998 / resolver.ts
Created August 28, 2020 02:56
resolveWhenAllWorkersAreStarted (resolve when callback is called N times)
const resolveWhenAllWorkersAreStarted = new class {
private promise: Promise<void>
private resolve!: VoidFunction;
private readyWorkerNumber = 0;
public constructor (private targetWorkerNumber: number) {
this.promise = new Promise<void>((resolve) => {
this.resolve = resolve
})
this.workerReadyMessageListener = this.workerReadyMessageListener.bind(this)
@lightyears1998
lightyears1998 / index.ts
Created August 27, 2020 08:31
Node.js 的 cluster 模块提供的语法糖
import cluster from "cluster"
function showEventsAndListener(processName: string) {
console.log(
`${processName} events and listeners:`,
process.eventNames().map(e => `${e.toString()}(${process.listenerCount(e)})`).join(', ')
)
}
@lightyears1998
lightyears1998 / source.cpp
Created May 25, 2020 03:31
银行家算法演示程序
// 银行家算法演示程序
#include "third-party-table_printer.h"
#include <cassert>
#include <ctime>
#include <vector>
#include <iostream>
using namespace std;
@lightyears1998
lightyears1998 / typing-practice.cpp
Last active December 17, 2017 06:30
Great Template of Typing Practice of ACMers
/* Great template of typing practice for ACMers */
#include <ctime>
#include <iostream>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b)
{
return b == 0 ? a : gcd(b, a % b);
}