Skip to content

Instantly share code, notes, and snippets.

View limboinf's full-sized avatar
🎯
Focusing

limbo limboinf

🎯
Focusing
View GitHub Profile
@limboinf
limboinf / jsonhandler.py
Created August 18, 2016 04:40 — forked from mminer/jsonhandler.py
A JSON request handler for Tornado.
import json
import tornado.web
class JsonHandler(BaseHandler):
"""Request handler where requests and responses speak JSON."""
def prepare(self):
# Incorporate request JSON into arguments dictionary.
if self.request.body:
try:
@limboinf
limboinf / app.js
Created August 17, 2016 08:27
《Nodejs实战》TOP3继承并扩展EventEmitter事件处理文件监听
var Watch = require('./fswatch'),
fs = require('fs'),
watchDir = './watch',
processDir = './done';
var watcher = new Watch(watchDir, processDir);
watcher.on('process', function (file) {
var watchFile = this.watchDir + '/' + file;

本文基本上这为两篇文章的翻译和整合 -- Scalable networking And Why are event-driven server so great

OPPC模型瓶颈

传统服务器模型如Apache为每一个请求生成一个子进程。当用户连接到服务器的一个子进程就产生,并处理连接。每个连接获得一个单独的线程和子进程。当用户请求数据返回时,子进程开始等待数据库操作返回。如果此时另一个用户也请求返回数据,这时就产生了阻塞。

这种模式在非常小的工作负荷是表现良好,当请求的数量变得太大是服务器会压力过于巨大。 当Apache达到进程的最大数量,所有进程都变得缓慢。每个请求都有自己的线程,如果服务代码使用PHP编写时,每个进程所需要的内存量是相当大的[1]。

fork()操作延时

@limboinf
limboinf / node-tcp-chat.js
Created July 30, 2016 06:54
nodejs 简单的Chat demo.
/**
* Created by fangpeng on 16/7/30.
* node app.js
* Usage: telnet localhost 9000
*/
var net = require("net");
//创建TCP服务器
var chatServer = net.createServer(),
clientList = []; // 保存所有连接的客户端
@limboinf
limboinf / malloc_and_qsort_array.c
Created July 27, 2016 09:40
《C和指针》TOP11 为一个指定的数组分配内存并排序处理。
#include <stdio.h>
#include <stdlib.h>
int comp(const void *, const void *);
int main()
{
int *array;
int n_values;
int i;
@limboinf
limboinf / simple-event-loop.py
Created July 11, 2016 13:37
下面是一个简单事件驱动实现Echo服务器。 事件驱动的核心就是都有一个循环来轮询socket的活跃性并执行响应。 事件驱动型I/O的一个潜在的优势在于它可以在不使用线程或进程的情况下同时处理成百上千个socket。 事件循环一次处理一个事件,不需要任何其他的并发原语参与。 事件驱动型I/O的缺点在于并没有涉及真正的并发。如果任何一个事件处理方法阻塞了或者 执行了一个耗时较长的计算,那么就会阻塞整个程序的执行进程。 对于阻塞型或者需要长时间运行的计算,可以通过将任务发送给单独的线程或进程来解决。 但是将线程和进程同事件循环进行协调需要较高的技巧。常常用concurrent.futures模块来实现。
# coding=utf-8
"""
《Python Cookbook》11.12 理解事件驱动型I/O
事件驱动I/O是一种将基本的I/O操作(即:读和写)转换成事件的技术,而我们必须在程序中去处理这种事件。
比如当socket上都到数据时,这成为一个"接收事件", 由我们提供的回调方法或函数去负责处理以此响应这个事件。
一个事件驱动框架可能会以一个基类作为起始点,实现一系列基本的事件处理方法。
:copyright: (c) 2016 by fangpeng(@beginman.cn).
:license: MIT, see LICENSE for more details.
"""
@limboinf
limboinf / tornado-async-spider.py
Last active June 12, 2018 08:21
tornado异步爬虫示例
# coding=utf-8
"""
tornado异步爬虫示例
"""
import time
from datetime import timedelta
from bs4 import BeautifulSoup
from tornado.httpclient import AsyncHTTPClient
from tornado import ioloop, gen, queues
@limboinf
limboinf / two-ways-to-check-req-fields.py
Last active July 4, 2016 09:19
Two ways to check the request of the integrity of the fields
# coding=utf-8
"""
Two ways to check the request of the integrity of the fields
valid_req_field: Primitive way
valid_req_field_by_compress: itertools.compress()
"""
import itertools
def valid_req_field(req_data, *args):
@limboinf
limboinf / go_select_fib.go
Created July 4, 2016 05:35
go select with fibonacci
package main
import (
"fmt"
)
func fib(c, quit chan int) {
x, y := 0, 1
// select 语句使得一个goroutine在多个通讯操作上等待
// select 会阻塞,直到条件分支中的某个可以继续执行,如果有多个可执行,则随机执行一个
@limboinf
limboinf / pretty_main.py
Created July 1, 2016 09:58
Python之父教你写main()函数.
# coding=utf-8
"""
Python之父教你写main()函数
----------------------------------
main()函数使其接受一个可选参数 argv,支持在交互式shell中调用该函数,
我们就可以动态地提供 argv 的值。
让main()函数的返回值指示退出状态(exit status)并且,
main()函数中的sys.exit(n)调用全部变成return n。