GIL下CPU 使用超过100%
·其实是逃出了GIL的魔掌
·走进了C的怀抱
from ctypes import cdll
from threading import Thread
import time
lib = cdll.LoadLibrary(“./a.so”)
Thread(target=lib.loop).start()
Thread(target=lib.loop).start()
· 最好还是使用multiprocessing模块
多进程下的异常输出
· 无论是多进程还是多线程 都应该使用logging模块
logger = logging.getLogger(filename)
filename = ‘xxx’
logformat = logging.Formatter(‘%(asctime)s [%(levelname)s] %(message)s’,’%Y-%m-%d %H:%M:%S’)
logger.setLevel(getattr(logging, level))
fh = logging.FileHandler(filename)
fh.setFormatter(logformat)
logger.addHandler(fh)
· 想要traceback
· 可以这样
def traceback_wrap(self):
def tt():
import traceback as tb
class MimicryFile:
def init(self, log):
self.log= log
def write(self, strr):
self.log.error(strr.strip())
tb.print_exc(file=MimicryFile(self))
return tt
logger.traceback = traceback_wrap(logger)
logger.traceback() # usage
· 函数参数拆包
In [21]: def a(x, y):
…: print(x, y)
…:
In [22]: i = [1,2]
In [23]: j = {“y”:3,”x”:2}
In [24]: a(*i)
1 2
In [25]: a(**j)
2 3
· 不定长参数
In [33]: def a(*x, **y):
…: print(x, y)
…:
…:
In [34]: a(1,2,3,x=2,y=3)
(1, 2, 3) {‘x’: 2, ‘y’: 3}
· 链式比较
In [1]: x = 2
In [2]: y = 2.5
In [3]: 1 < x < y < 3
Out[3]: True
· for else
In [4]: for i in range(10):
…: if i == 10:
…: break
…: else:
…: print (‘0~9’)
…: