python进阶知识【二】

1.pyhton中的进程,线程,协程

https://www.cnblogs.com/xuyaping/p/6825115.html(IO模型有点没有完全明白)


补充:在文中的setDaemon,守护线程部分,我有一点分歧。原文如下:

import threading
from time import ctime,sleep
 
def Music(name):
        print ("Begin listening to {name}. {time}".format(name=name,time=ctime()))
        sleep(3)
        print("end listening {time}".format(time=ctime()))
 
def Blog(title):
        print ("Begin recording the {title}. {time}".format(title=title,time=ctime()))
        sleep(5)
        print('end recording {time}'.format(time=ctime()))
 
threads = []
 
t1 = threading.Thread(target=Music,args=('FILL ME',))
t2 = threading.Thread(target=Blog,args=('python',))
 
threads.append(t1)
threads.append(t2)
 
if __name__ == '__main__':
    t1.setDaemon(True)  # 注意:一定在start之前设置
    t1.start()
    t2.start()
 
    print ("all over %s" %ctime())
 
--->Begin listening to FILL ME. Mon May  8 17:51:47 2017
    Begin recording the python. Mon May  8 17:51:47 2017
    all over Mon May  8 17:51:47 2017
    end listening Mon May  8 17:51:50 2017
    end recording Mon May  8 17:51:52 2017        #因为t1运行时间比较长,所以t1运行完其他线程也都运行完毕。

我觉得将t1设为守护线程,t1会随着主线程的退出而退出,t1执行时间比t2执行时间短。由于t2不是守护线程,所以主线程会等待t2执行完毕,才退出。而在t2执行的过程中,t1可以执行完毕。所以从结果看,主线程结束后,t2执行完毕才退出。(自我的理解请大伙批评指正)

同理原文的下一个,将t2设为守护线程

import threading
from time import ctime,sleep
 
def Music(name):
        print ("Begin listening to {name}. {time}".format(name=name,time=ctime()))
        sleep(3)
        print("end listening {time}".format(time=ctime()))
 
def Blog(title):
        print ("Begin recording the {title}. {time}".format(title=title,time=ctime()))
        sleep(5)
        print('end recording {time}'.format(time=ctime()))
 
threads = []
 
t1 = threading.Thread(target=Music,args=('FILL ME',))
t2 = threading.Thread(target=Blog,args=('python',))
 
threads.append(t1)
threads.append(t2)
 
if __name__ == '__main__':
    t2.setDaemon(True)  # 注意:一定在start之前设置
    t1.start()
    t2.start()
 
    print ("all over %s" %ctime())
 
--->Begin listening to FILL ME. Mon May  8 17:54:44 2017
    Begin recording the python. Mon May  8 17:54:44 2017
    all over Mon May  8 17:54:44 2017
    end listening Mon May  8 17:54:47 2017   #因为t2进程运行只有3s,而t1进程运行需要5s,所以当t2进程和主进程运行完毕,整个程序就结束,不管t1是否运行完毕。

因为t1执行需要3秒,t2需要5秒,t2为守护线程,会随着主线程的退出而退出。

死锁

import threading
import time
 
mutexA = threading.Lock()
mutexB = threading.Lock()
 
class MyThread(threading.Thread):
 
    def __init__(self):
        threading.Thread.__init__(self)
 
    def run(self):
        self.fun1()
        self.fun2()
 
    def fun1(self):
 
        mutexA.acquire()  # 如果锁被占用,则阻塞在这里,等待锁的释放
 
        print ("I am %s , get res: %s---%s" %(self.name, "ResA",time.time()))
 
        mutexB.acquire()
        print ("I am %s , get res: %s---%s" %(self.name, "ResB",time.time()))
        mutexB.release()
        mutexA.release()
 
 
    def fun2(self):
 
        mutexB.acquire()
        print ("I am %s , get res: %s---%s" %(self.name, "ResB",time.time()))
        time.sleep(0.2)
 
        mutexA.acquire()
        print ("I am %s , get res: %s---%s" %(self.name, "ResA",time.time()))
        mutexA.release()
 
        mutexB.release()
 
if __name__ == "__main__":
 
    print("start---------------------------%s"%time.time())
 
    for i in range(0, 10):
        my_thread = MyThread()
        my_thread.start()

我的理解:在线在运行fun1,fun2时,mutexA锁被占用,mutexB锁也被占用,fun1中的mutexA要继续往下运行,就要获得mutexB,但此时mutexB在fun2中被占用,要想释放fun2中的mutexB,就要往下运行,现获得mutexA,而此时,mutexA在fun1中被占用。所以卡死。

参考:

https://blog.csdn.net/qq_42486920/article/details/81545342

https://blog.csdn.net/mike_shine/article/details/80938112

https://www.cnblogs.com/DjangoBlog/p/5783144.html

https://blog.csdn.net/qq_33733970/article/details/77763009

2.详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数

https://www.jb51.net/article/138363.htm

3.python - @staticmethod和@classmethod的作用与区别

https://blog.csdn.net/handsomekang/article/details/9615239

4.python多个装饰器的用法原理

https://blog.csdn.net/jyhhhhhhh/article/details/54627850

5.function.wraper 装饰器

https://blog.csdn.net/hqzxsc2006/article/details/50337865

http://python.jobbole.com/86687/

6.python中queue的操作:

https://blog.csdn.net/linangfs/article/details/78109374

https://www.cnblogs.com/skiler/p/6977727.html

http://python.jobbole.com/84039/             ----------queue的实现

猜你喜欢

转载自blog.csdn.net/qq_35810838/article/details/82819914