- python運行原理
使用cpython解釋器創(chuàng)建棧幀(堆)寨昙,建立上下文装获,字節(jié)碼對象
由于棧幀分配在內(nèi)存中篙梢,所以棧幀可以獨立于調(diào)用者存在吼砂,即使調(diào)用完畢依然可以訪問
import dis
dis.dis(object) #查看棧幀
------------
def foo(): bar()
def bar():
global frame
frame = inspect.currentframe()
frame.f_back #調(diào)用函數(shù) foo()
frame.f_code.co_name # 內(nèi)部執(zhí)行函數(shù) bar()
- 迭代器對象逆航,可迭代對象
迭代器 iter, next
可迭代對象 iter, getitem
l = [1,2,3]
for i in l: print i
s = '2323'
for x in s: print x
可迭代對象得到迭代器對象
iter(l)
<listiterator at 0x...>
iter(l) 調(diào)用了 l.__iter__()
---------
s.__getitem__() #獲取序列
----------
# 得到一個迭代器
t = iter(l)
t.next()
-----------
#實現(xiàn)一個迭代器對象next
#實現(xiàn)一個可迭代對象__iter__方法返回一個迭代器對象
from collections import Iterable, Iterator
# 抽象接口
Iterator.__abstractmethods__ #next
Iterable.__abstractmethods__ #__iter__
#迭代器
class MyIterator(Iterator):
def __init__(self, cities):
self.cities = cities
self.index = 0
def getcity(self, city):
# 處理
return city
def next(self):
if self.index == len(self.cities):
raise StopIteration
city = self.cities[self.index]
"""
try:
city = self.cities[self.index]
except IndexError:
raise StopIteration
"""
self.index += 1
return self.getcity(city)
#可迭代對象
class MyIterable(Iterable):
def __init__(self, cities):
self.cities = cities
def __iter__(self):
return MyIterator(self.cities)
#def __getitem__(self, item):
# return self.cities[item]
#實例化
for i in MyIterable(['beijing','xian']):
print i
# for 實現(xiàn)
myIter = iter(MyIterable(['beijing','xian']))
while True:
try:
print next(myiter)
except Stoplteration:
pass
- 生成器(協(xié)程) yield 既有可迭代對象,又有迭代器的特性
使用生成器函數(shù)實現(xiàn)可迭代對象
def f():
print 'in f()', 1
yield 1
print 'in f()', 2
yield 2
g = f() #生成器對象渔肩,在編譯字節(jié)碼的使用處理
g.next() #1
g.next() #2
#Stopltreation
------------
for x in g:
print x
------------
g.__iter__() is g # True
__iter__() 還是生成器類型因俐,使用yield進行迭代器next
------------
def fib(index):
if index <= 2:
return 1
else:
return fib(index-1) + fib(index-2) #只有最后結果
def fib(index):
ans = []
n, a, b = 0,0,1
while n < index:
ans.append(b)
a, b = b, a+b
n += 1
return ans
def fib2(index):
n, a, b = 0,0,1
while n < index:
yield b #得到每步的值
a, b = b, a+b
n += 1
for i in fib2(10):
print i
--------------
def gen_func():
yield 1
print "hello"
gen = gen_func()
print gen.gi_frame.f_lasti #記錄執(zhí)行最后位置,函數(shù)運行控制
print gen.gi_frame.f_locals #維護著當前生成器中的屬性字段
生成器案例:看源碼 from collections import UserList
大文件讀取寫入數(shù)據(jù)庫, 100g周偎,只有一行抹剩,特殊分隔符;
def myreadline(f, newline):
buf = ""
while True:
while newline in buf:
pos = buf.index(newline)
yield = buf[:pos]
#解析讀取文件過長
buf = buf[pos+len(newline):]
chunk = f.read(4096)
# 文件結尾
if not chunk:
yield buf
break
# 讀取短
buf += chunk
with open('a') as f:
for line in myreadline(f, '{|}'):
print line
- 線程(io密集)蓉坎,進程(cpu密集)澳眷,gil(global interpreter lock 全局解釋鎖),協(xié)程
GIL: 一個線程對應c語言一個線程 cpython
同一時刻只有一個線程在一個cpu上執(zhí)行字節(jié)碼蛉艾,無法多個線程映射多個cpu钳踊,為了保障線程安全,但不是絕對勿侯,因為gil對于cpu的占用會有釋放(根據(jù)字節(jié)碼行數(shù)|時間片)拓瞪,io操作會釋放
https://zhuanlan.zhihu.com/p/110406882
http://www.dabeaz.com/python/UnderstandingGIL.pdf
死鎖:1. 不釋放 2. 資源競爭
demo:
#加鎖 from threading import Lock,RLock
#可重入的鎖,同一個線程里可以助琐,可以多次調(diào)用acquire,配合使用release
#lock = Lock()
total = 0
def add():
global total
# global lock
for i in range(1000000):
#lock.acquire()
total += 1
#lock.release() #不釋放死鎖
def desc():
global total
#加鎖
for i in range(1000000):
total -= 1
import threading
t1 =threading.Thread(target=add)
t2 =threading.Thread(target=desc)
t1.start()
t2.start()
t1.join()
t2.join()
#total 隨機
還有線程同步祭埂,線程池,進程通信兵钮,進程池
線程:threading
thread = threading.Thread(target=test)
thread.start()
thread.join()
進程:mutilprocess
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()
協(xié)程:
阻塞(函數(shù)調(diào)用蛆橡,函數(shù)被當前線程掛起)舌界,非阻塞(立即返回),并發(fā)航罗, 并行禀横,同步(發(fā)送消息,等待IO)粥血,異步
IO多路復用 socket柏锄,select,epoll复亏,poll
io模型:阻塞io趾娃,非阻塞io, io多路復用,信號驅動io缔御,異步io匿名函數(shù)lambda
列表表達式
裝飾器 *args *kargs
為多個函數(shù)添加同一個功能
def fib(n):
if n <=1:
return 1
return fib(n-1) + fib(n-2)
----------
def fib(n):
if cache is None:
cache = {}
if n in cache:
return cache[n]
if n <= 1:
return 1:
cache[n] = fib(n-1, cache) + fib(n-2, cahce)
return cache[n]
------
def memo(func):
cache = {}
def warp(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return warp
#fib = memo(fib)
@memo
def fib(n):
...
- 單例模式
class single(object):
_instances = None
def __new__(cls, *args, **kargs):
if cls._instances is None:
cls._instances = object.__new__(cls, *args, **kargs)
return cls._instances
def __init__(self):
pass
- 類的加載
- 常用庫 requests, collections, re, MySQLdb,
https://docs.python.org/zh-cn/3.7/library/index.html - 常用框架 celery , django