摘要: 装饰器本质上就是一个闭包函数,装饰器有且只有一个参数,参数为要装饰的函数 作用:不改变原函数,而增强其功能 def decorator(fn): def inner(): print('已登录') fn() return inner @decorator def work(): print('可以进 阅读全文
posted @ 2021-03-09 19:17 code-G 阅读(82) 评论(0) 推荐(0) 编辑
摘要: 闭包的形成条件: 函数嵌套 内部函数使用外部函数的变量或者参数 外部函数返回内部函数。这个使用外部函数变量的内部函数称为闭包 按照容易理解的意思分析一下 func_out()的返回值是func_inner函数,所以func指代的就是func_inner函数 调用func函数,实际上调用的是func_ 阅读全文
posted @ 2021-03-09 18:54 code-G 阅读(65) 评论(0) 推荐(0) 编辑
摘要: 死锁:两个或两个以上的进程(线程)在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,程序无法推进 from threading import * lock = Lock() def get_value(index): lock.acquire() list1 = [1,2,3,4] 阅读全文
posted @ 2021-03-09 18:28 code-G 阅读(191) 评论(0) 推荐(0) 编辑
摘要: 想知道一些原理,建议去学操作系统 from threading import * num = 0 # 创建锁对象 lock = Lock() def task1(): # 上锁 lock.acquire() global num for i in range(1000000): num += 1 p 阅读全文
posted @ 2021-03-09 18:21 code-G 阅读(83) 评论(0) 推荐(0) 编辑
摘要: 进程是由线程组成的 进程之间全局变量不共享 线程之间资源共享 进程和线程的执行是无序的 # 进程和线程的执行是无序的 from multiprocessing import * from time import * from threading import * def print_info(): 阅读全文
posted @ 2021-03-09 18:16 code-G 阅读(97) 评论(0) 推荐(0) 编辑
摘要: 全局变量共享 from threading import * from time import * list1 = [] def add_data(): for i in range(3): list1.append(i) print('add',list1) sleep(0.2) def read 阅读全文
posted @ 2021-03-09 18:03 code-G 阅读(441) 评论(0) 推荐(0) 编辑
摘要: 主线程等待子线程执行完再结束 # 导包 from threading import * from time import * def dance(): print(current_thread()) for i in range(6): print('跳舞...') sleep(0.2) if __ 阅读全文
posted @ 2021-03-09 17:56 code-G 阅读(1129) 评论(0) 推荐(0) 编辑
摘要: # 导包 from threading import * from time import * def dance(): print(current_thread()) for i in range(3): print('跳舞...') sleep(0.2) def sing(): print(cu 阅读全文
posted @ 2021-03-09 17:52 code-G 阅读(115) 评论(0) 推荐(0) 编辑
摘要: 主进程或等待子进程执行完 # 输出over后主进程内容已经执行完了,但是会等待子进程执行完 from multiprocessing import * from time import * def print_info(): for i in range(10): print(i) sleep(0. 阅读全文
posted @ 2021-03-09 17:47 code-G 阅读(1084) 评论(0) 推荐(0) 编辑
摘要: from multiprocessing import * list1 = [] def add_data(): for i in range(3): list1.append(i) print('add ', list1) def read_data(): print('read ', list1 阅读全文
posted @ 2021-03-09 17:34 code-G 阅读(258) 评论(0) 推荐(0) 编辑