最近准备去面试了,总结一下可能会问到的知识点
python 问的最多的就是多线程了,GIL 锁,
有的还可能会问进程(通信,生产者消费者模式),协程,异步
垃圾回收,上下文,锁(乐观锁,悲观锁),事务,队列
还有一个链表这个可能问的比较少
一些基本的算法,力扣上面的,去重,排序,数组合并,双指针(对撞指针,快慢指针)
import threading
import time
'''
@File : my_thread.py
@Time : 2022/02/17 13:59:59
@Author : Kevin.zhao
@Version : 1.0
@Desc : 线程
'''
money = 1
def Order(n):
global money
money = money + n
money = money - n
class thread(threading.Thread):
def __init__(self, threadname):
threading.Thread.__init__(self, name='线程' + threadname)
self.threadname = int(threadname)
def run(self):
for i in range(100):
lock.acquire() # 死锁
Order(self.threadname)
lock.release() #解锁
# print('%s:Now timestamp is %s'%(self.name,time.time()))
lock = threading.Lock()
t1 = thread('1')
t2 = thread('5')
t1.start() #开启线程
t2.start()
t1.join() # 阻塞线程,相当于集合点
t2.join()
print(money)
import threading,time
def TestA():
cond.acquire()
print('李白:看见一个敌人,请求支援')
cond.wait() # 线程挂起,直到收到一个 Notify() 通知或者超时(可选参数),wait() 必须在线程得到 Rlock 后才能使用
print('李白:好的')
cond.notify()
cond.release()
def TestB():
time.sleep(2)
cond.acquire()
print('亚瑟:等我...')
cond.notify() #在线程挂起的时候,发送一个通知,让 wait() 等待线程继续运行,Notify() 也必须在线程得到 Rlock 后才能使用。 Notify(n=1),最多唤醒 n 个线程。
# NotifyAll 在线程挂起的时候,发送通知,让所有 wait() 阻塞的线程都继续运行。
cond.wait()
print('亚瑟:我到了,发起冲锋...')
if __name__=='__main__':
cond = threading.Condition()
testA = threading.Thread(target=TestA)
testB = threading.Thread(target=TestB)
testA.start()
testB.start()
testA.join()
testB.join()
from multiprocessing import Process,Queue
import time,random,os
'''
@File : my_process.py
@Time : 2022/02/17 15:16:59
@Author : Kevin.zhao
@Version : 1.0
@Desc : 进程
'''
def consumer(q):
while True:
res=q.get()
if res is None:
break #收到结束信号则结束
time.sleep(random.randint(1,3))
print(f'吃{os.getpid()}{res}')
def producer(q):
for i in range(10):
time.sleep(random.randint(1,3))
res='包子%s' %i
q.put(res)
print(f'生产{os.getpid()}{res}')
q.put(None) #发送结束信号
if __name__ == '__main__':
q=Queue()
#生产者们:即厨师们
p1=Process(target=producer,args=(q,))
#消费者们:即吃货们
c1=Process(target=consumer,args=(q,))
#开始
p1.start()
c1.start()
print('主')
import asyncio
import time
'''
@File : my_asyncio.py
@Time : 2022/03/08 16:56:23
@Author : Kevin.zhao
@Version : 1.0
@Desc : None
'''
async def coroutine_example(name):
print('正在执行name:', name)
# await asyncio.sleep(1)
print('执行完毕name:', name)
loop = asyncio.get_event_loop()
tasks = [coroutine_example("一号"),coroutine_example("二号"),coroutine_example("三号"),coroutine_example("四号")]
wait_coro = asyncio.wait(tasks)
loop.run_until_complete(wait_coro)
loop.close()
import contextlib
'''
@File : context.py
@Time : 2022/03/08 16:57:34
@Author : Kevin.zhao
@Version : 1.0
@Desc : 上下文
'''
class Resource():
def __enter__(self):
print('===connect to resource===')
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print('===close resource connection===')
def operate(self):
print('===in operation===')
with Resource() as res:
res.operate()
@user21nager
def open_func(file_name):
# __enter__方法
print('open file:', file_name, 'in __enter__')
file_handler = open(file_name, 'r')
# 【重点】:yield
yield file_handler
# __exit__方法
print('close file:', file_name, 'in __exit__')
file_handler.close()
return
with open_func('/Users/MING/mytest.txt') as file_in:
for line in file_in:
print(line)