Skip to content

Instantly share code, notes, and snippets.

View justdoit0823's full-sized avatar

余森彬 justdoit0823

View GitHub Profile
@justdoit0823
justdoit0823 / deadlock-after-fork-in-python.py
Created July 2, 2017 13:44
Deadlock in python after fork
import multiprocessing
import os
import threading
import time
def child_thread(lock):
with lock:
time.sleep(10)
#include <Python.h>
#include "stdio.h"
int main(int argc, char * argv[]){
int i = 0;
PyTypeObject ptype, btype = PyType_Type, PyBaseObject_Type;
printf("i 0x%lx.\n", (unsigned long)&i);
printf("type name %s.\n", PyType_Type.tp_name);
@justdoit0823
justdoit0823 / type_getattr.c
Last active August 28, 2017 11:28
How Python resolves type attribute access
/* This is similar to PyObject_GenericGetAttr(),
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
static PyObject *
type_getattro(PyTypeObject *type, PyObject *name)
{
PyTypeObject *metatype = Py_TYPE(type);
PyObject *meta_attribute, *attribute;
descrgetfunc meta_get;
if (!PyUnicode_Check(name)) {
@justdoit0823
justdoit0823 / object_getattr.c
Last active August 28, 2017 11:28
How Python resolves object attribute access
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
PyObject *
_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *descr = NULL;
PyObject *res = NULL;
descrgetfunc f;
Py_ssize_t dictoffset;
@justdoit0823
justdoit0823 / object_setattr.c
Created July 6, 2017 02:26
How Python object set attribute
// type object set attribute
static int
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(
PyExc_TypeError,
"can't set attributes of built-in/extension type '%s'",
type->tp_name);
@justdoit0823
justdoit0823 / grpc_fixture.py
Created July 6, 2017 12:15
A simple grpc test fixture in pytest.
from concurrent.futures import ThreadPoolExecutor
import grpc
import pytest
import a_pb2_grpc
from rpc.server import AGRpcServer, BGRpcServer
import b_pb2_grpc
@justdoit0823
justdoit0823 / sqlalchemy_utils.py
Created July 10, 2017 09:10
How to get new primary key value after flush session in sqlalchemy
from sqlalchemy.orm.base import instance_state
def get_instance_identity(instance, single=True):
"""获取实例主键值。"""
identity = instance_state(instance).identity
if single:
return identity[0]
@justdoit0823
justdoit0823 / install_common_libdev.sh
Last active August 8, 2017 06:00
Common lib development dependencies.
# install zlib
apt install zlib1g zlib1g-dev
# install pcre
apt install libpcre3 libpcre3-dev
# language zh-hans
apt install language-pack-zh-hans
@justdoit0823
justdoit0823 / multi_update_same_row.sql
Created July 17, 2017 15:10
Update same row multiple times in postgresql
drop table if exists s_multi_update_same_row;
create table s_multi_update_same_row(
id int,
count int
);
insert into s_multi_update_same_row select id, 0 from generate_series(1, 1000) as id;
update s_multi_update_same_row A set count = count + 1 from generate_series(1, 10000) as B(id) where A.id = B.id % 1000 + 1;
@justdoit0823
justdoit0823 / hll-in-postgresql.sql
Created July 18, 2017 04:38
How hll works in postgresql
drop table if exists s_user_table;
create table s_user_table (
id int,
user_id int
);
drop table if exists s_user_cnt_table;
create table s_user_cnt_table (