随笔分类 -  python基础

python基础
摘要:import geventfrom gevent import monkeyimport time# monkey.patch_all() # 补丁包,协程有IO等待时,触发协程切换def work1(): for i in range(10): print(f' work1 {i}') # tim 阅读全文
posted @ 2022-04-24 17:08 狒狒桑 阅读(31) 评论(0) 推荐(0) 编辑
摘要:from greenlet import greenletdef work1(): for i in range(10): print(f' work1 {i}') g2.switch()def work2(): for i in range(10): print(f' work2 {i}') g1 阅读全文
posted @ 2022-04-24 16:48 狒狒桑 阅读(18) 评论(0) 推荐(0) 编辑
摘要:from queue import Queueq = Queue()def func1(): while not q.empty(): i = q.get() print(f' func1 {i}') yield idef func2(): while not q.empty(): i = q.ge 阅读全文
posted @ 2022-04-24 16:36 狒狒桑 阅读(28) 评论(0) 推荐(0) 编辑
摘要:进程与进程之间不共享全局变量 from multiprocessing import Process, Queueimport time,osdef func1(q:Queue): while not q.empty(): print(' func1 {0} {1}'.format(q.get(), 阅读全文
posted @ 2022-04-24 15:21 狒狒桑 阅读(25) 评论(0) 推荐(0) 编辑
摘要:from multiprocessing import Pool, Managerimport time,osdef func1(q): print(' func1 {0} {1}'.format(q.get(), os.getpid())) time.sleep(1)if __name__ == 阅读全文
posted @ 2022-04-24 15:21 狒狒桑 阅读(20) 评论(0) 推荐(0) 编辑
摘要:import queuefrom threading import Threadimport timeq = queue.Queue()class Productor(Thread): def run(self) -> None: count = 0 while True: if q.qsize() 阅读全文
posted @ 2022-04-24 13:03 狒狒桑 阅读(24) 评论(0) 推荐(0) 编辑
摘要:队列: import queue# 1. 先进先出q1 = queue.Queue(5)q1.put(11)q1.put(22, block=False) # 插入不等待q1.put_nowait(33) # 插入不等待print(q1.qsize()) # 队列长度q1.get()q1.get(b 阅读全文
posted @ 2022-04-24 12:32 狒狒桑 阅读(31) 评论(0) 推荐(0) 编辑
摘要:线程效率: 1、CPU计算密集型:单线程 比多线程 快 2、网络IO密集型:多线程 比 单线程 快 import requestsimport threadingimport timenum = 0def func1(): for i in range(100): # global num # nu 阅读全文
posted @ 2022-04-24 10:42 狒狒桑 阅读(295) 评论(0) 推荐(0) 编辑
摘要:多个线程互相等待锁,会导致死锁 import threadingimport timemetalocA = threading.Lock()metalocB = threading.Lock()num = 0def func1(): for i in range(1000000): global n 阅读全文
posted @ 2022-04-24 10:32 狒狒桑 阅读(25) 评论(0) 推荐(0) 编辑
摘要:加锁的作用: 1、多个线程同时修改同一个全局变量时,确保不被CPU切片阻断 2、加锁会加长执行时间 import threadingimport timemetalocA = threading.Lock()# metalocB = threading.Locknum = 0def func1(): 阅读全文
posted @ 2022-04-24 10:23 狒狒桑 阅读(226) 评论(0) 推荐(0) 编辑
摘要:import threadingimport timeimport requestsclass MyThreading(threading.Thread): def __init__(self, url): self.url = url # 给run方法传参,只能通过 self的属性 super() 阅读全文
posted @ 2022-04-23 22:24 狒狒桑 阅读(43) 评论(0) 推荐(0) 编辑
摘要:import threadingimport timefrom time import sleepdef func1(): for i in range(4): sleep(1) print(' func1 ', threading.current_thread())def func2(): for 阅读全文
posted @ 2022-04-23 22:04 狒狒桑 阅读(17) 评论(0) 推荐(0) 编辑
摘要:(1)使用type直接创建类 Test = type('Test01', (object,), {'name': 'haha', 'age': 18})t1 = Test()print(type(t1))print(Test.__dict__) (2)继承type,用继承类创建类 class MyT 阅读全文
posted @ 2022-04-23 18:24 狒狒桑 阅读(18) 评论(0) 推荐(0) 编辑
摘要:(1)描述器类 class Field: # 设置描述器类对象属性时调用 def __set__(self, instance, value): print(' __set__ ') self.value = value # 获取描述器对象属性时调用 def __get__(self, instan 阅读全文
posted @ 2022-04-23 11:15 狒狒桑 阅读(18) 评论(0) 推荐(0) 编辑
摘要:class MyClass(object): # 设置对象属性时调用 def __setattr__(self, key, value): print(' __setattr__ ') super().__setattr__(key, value) # 访问属性不存在,__getattribute_ 阅读全文
posted @ 2022-04-23 10:58 狒狒桑 阅读(18) 评论(0) 推荐(0) 编辑
摘要:(1)私有属性 class MyClass: attr1 = 'attr1' # 共有属性 _attr2 = '_attr2' # 私有属性,可直接访问 __attr3 = '__attr3' # 私有属性,改变了属性名:_MyClass__attr3m1 = MyClass()print(m1.a 阅读全文
posted @ 2022-04-22 22:49 狒狒桑 阅读(26) 评论(0) 推荐(0) 编辑
摘要:class Animal: def run(self): print(' animal run ')class Cat(Animal): def run(self): print(' cat run ')class Dog(Animal): def run(self): print(' dog ru 阅读全文
posted @ 2022-04-22 22:39 狒狒桑 阅读(26) 评论(0) 推荐(0) 编辑
摘要:class MyOperator: def __init__(self, data): self.data = data def __add__(self, other): return self.data + other.data def __sub__(self, other): return 阅读全文
posted @ 2022-04-22 21:56 狒狒桑 阅读(24) 评论(0) 推荐(0) 编辑
摘要:(1)文件读取--上下文管理器 with open(filename,'w') as f : class MyOpen: def __init__(self, filename, mode, encoding='utf-8'): self.filename = filename self.mode 阅读全文
posted @ 2022-04-22 21:46 狒狒桑 阅读(22) 评论(0) 推荐(0) 编辑
摘要:class Decorator: def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): print(' call ') return self.func(*args, **kwargs)@Dec 阅读全文
posted @ 2022-04-22 18:58 狒狒桑 阅读(14) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示