Python Thread
# GIL
GIL global interpreter lock (cpython),python 中一个线程对应 C 语言的一个线程
GIL 使得同一时刻只有一个线程在一个 CPU 上执行,无法将多个线程映射到多个 CPU 上执行
GIL 会根据执行的字节码行数以及时间片释放 GIL ,在遇到 IO 操作时也会主动释放
运行
12345678910import threading, multiprocessingdef loop(): x = 0 while True: x = x ^ 1for i in range(multiprocessing.cpu_count()): t =...
more...
Python Wsgi
# 浏览器请求动态 web 页面过程
# WSGI
WSGI:Web Server Gateway Interface。
WSGI 接口定义非常简单,它只要求 Web 开发者实现一个函数,就可以响应 HTTP 请求。
123def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return [b'Hello,...
more...
Python XML Parsing
# ElementTree vs SAX and DOM
SAX (simple API for XML) It functions as a stream parser, with an event-driven API.
DOM (Document Object Model) It reads the XML into memory and converts it to objects that can be accessed with Python.
ElementTree
ElementTree is much easier to use, because it represents...
more...
GraphQL Sample
# GraphQL 特点
精确请求需要的数据,不多不少。例如: account 中有 name, sex, age 可以只取得需要的字段 name。
可以只用一个请求获取多个资源。
描述所有可能的类型系统,方便维护,可以根据需求平滑演进,添加或隐藏字段。
# GraphQL 与 restful 区别
restful 一个接口只能返回一个资源,graphql 一次可以获得多个资源
restful 使用不同 url 来区分资源,graphql 使用类型区分资源。
# GraphQL 参数类型
基本类型: String, Int, Float, Boolean, ID;可以在 schema...
more...
JavaScript Prototype
1234567891011121314151617181920212223242526272829303132Number.prototype.add = function (x) { return this + x;};Number.prototype.subtract = function (x) { return this - x;};Number.prototype.iterate = function () { var result = []; for (var i = 0; i...
more...
JavaScript Create Object
javascript 对象创建方式
# prototype
12345678910function Cat() { this.name = "mm";}Cat.prototype.makeSound = function () { console.log("o(∩_∩)o");};var cat = new Cat();cat.makeSound();
#...
more...
JavaScript Core
# IIFE
Immediately Invoked Function Expression, 立即执行函数表达式。
123(function () { console.log("IIFE");})();
# closure
闭包:外部函数返回后,内部函数仍然可以访问外部函数变量。
123456789101112131415function outer() { let n = 0; function inner() { n += 1; console.log(n); }...
more...
Node Async
# Promise
# resolve,reject
12345678910111213141516171819202122232425function test(resolve, reject) { var timeOut = Math.random() * 2 console.log('timeOut: ' + timeOut) setTimeout(function() { if (timeOut < 1) {...
more...