Skip to content

Instantly share code, notes, and snippets.

View polebug's full-sized avatar
🎯
Think it twice

polebug polebug

🎯
Think it twice
View GitHub Profile
@polebug
polebug / goroutine-pool.go
Created April 29, 2019 12:12
实现一个简单的线程池
package main
import (
"fmt"
"sync"
"time"
)
var jobs = make(chan int, 10)
var results = make(chan int, 10)
@polebug
polebug / producer-consumer-demo.go
Created April 29, 2019 10:27
Go 使用 goroutine 和 channel 实现一个简单的`生产者-消费者`模型
package main
import (
"fmt"
"time"
)
var channel chan int = make(chan int, 1024)
func producer(role string, channel chan<- int) {
@polebug
polebug / channel.go
Created April 29, 2019 10:06
Go 中 channel 的使用
package main
import (
"fmt"
"time"
)
// 使用 make 建立一个 channel
// 定义只能接受的 channel
@polebug
polebug / goroutine.go
Last active April 29, 2019 02:55
启动 goroutine 的简单代码
package main
import (
"fmt"
"runtime"
"time"
)
func getGoroutineInfo() {
NumCPU := runtime.NumCPU()
@polebug
polebug / test_defer.go
Last active April 29, 2019 00:19
go 中 defer 的用法
// defer 的用法:
// 当 defer 被声明时,其参数就会被实时解析
// defer 执行顺序为先进后出
// defer 可以读取有名返回值,可以改变有名返回参数的值
package main
import "fmt"
func testDefer(para int) int {
@polebug
polebug / func.go
Last active April 28, 2019 08:30
学习 Go 的代码片段
//函数不支持默认值参数,不支持函数重载
//不支持在函数里嵌套一个命名函数定义,但是支持嵌套匿名函数,for example: anonymous := func(){}
package main
import "fmt"
//函数类型
//两个函数类型相同指的是:形参列表和返回值列表`完全相同`
func funcType(para1, para2 int){
@polebug
polebug / WSGI_server&Python_socket.md
Created November 7, 2017 13:49
WSGI 协议实现与 Python socket 编程补充说明

偶然看到有人翻译的“一起写一个 web 服务器”系列[1][2][3],感觉挺不错的,这里做一些归纳和 Python socket 编程的补充说明。

WSGI 的工作原理

常见的 Web server 无法与 Web application(Flask, Django, Tornado) 直接通信,需要 WSGI server 作为桥梁。

WSGI 工作原理分为两方面

服务器层: 将来自 socket 的数据包解析为 http,调用 application,给应用提供环境信息 environ (包含 host, post, process模式, 客户端的 header, body 等)。同时给应用提供一个 start_response 的回调函数,主要在应用程序层进行响应信息处理。
@polebug
polebug / WSGI_server.py
Created November 7, 2017 13:10
WSGI server 基本实现
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import StringIO
import sys
class WSGIServer(object):
address_family = socket.AF_INET

Python Socket 编程详细介绍

Python 提供了两个基本的 socket 模块:

  • Socket 它提供了标准的BSD Socket API。
  • SocketServer 它提供了服务器重心,可以简化网络服务器的开发。

下面讲解下 Socket模块功能。

Socket 类型

@polebug
polebug / 0.md
Last active September 7, 2017 11:40
Most ThinkPython Exercises

上学期看了 ThinkPython,感觉后面的题目引申不错,于是写了写。
以及当时写了一些笔记,都扔在这里好了。

笔记:

对于列表:

列表的操作主要有find, add, 切片, 归并, sort/sorted, map, 筛选(filter), 删除, list, split, join
0.其中add操作有append(添加一个元素并返回None), extend(添加一个列表);
1.这里的归并的定义是:将一系列的元素合并成一个单一值的操作;
2.删除操作有, pop:将移除指定下标的元素并返回最后一个元素; del:移除指定下标的元素,可移除多个元素,结合切片索引使用;remove:直接删除值,无需知道下标;
3.list:将一个字符串转换为字符的列表;