python-threading学习笔记(一)

1、python中一个线程对应于C语言中的一个线程。

2、GIL使得同一时刻只有一个线程在一个cpu上执行字节码,无法将多个线程映射到多个cpu上执行。

3、GIL会根据执行的字节码行数以及时间片释放GIL,GIL在遇到io操作时会主动释放。

 

import threading
import time
def func_one():
    print("start_func_one")
    time.sleep(2)
    print("end_func_one")

def func_two():
    print("start_func_two")
    time.sleep(4)
    print("end_func_two")

if __name__ == "__main__":
    thread1 = threading.Thread(target=func_one)
    thread2 = threading.Thread(target=func_two)
    start_time =time.time()
    thread1.start()
    thread2.start()
    print("time:{}".format(time.time() - start_time))

根据执行结果,可以看到时间上不对,这是因为这儿计算的是主线程的执行时间,上面除了thread1和thread2 两个线程,还有个主线程,这儿计算的是主线程的时间。

(zabbixweb) D:\data\python\zabbixapiweb\project\project>d:/data/python/environment/zabbixweb/Scripts/python.exe d:/data/python/zabbixapiweb/project/project/test13.py     
start_func_one
start_func_two
time:0.0009965896606445312
end_func_one
end_func_two
 thread1.setDaemon(True)
 thread2.setDaemon(False)
import threading
import time
def func_one():
    print("start_func_one")
    time.sleep(2)
    print("end_func_one")

def func_two():
    print("start_func_two")
    time.sleep(4)
    print("end_func_two")

if __name__ == "__main__":
    thread1 = threading.Thread(target=func_one)
    thread2 = threading.Thread(target=func_two)
    #把子线程设置成守护线程(表示该线程是不重要的,进程退出时不需要等待这个线程执行完成。
    #这样做的意义在于:避免子线程无限死循环,导致退不出程序,也就是避免传说中的孤儿进程。),
    #即主线程终止时,守护也会终止,不管子线程运行到哪一步。 True表示置该线程为守护线程.
    thread1.setDaemon(True)
    thread2.setDaemon(False)
    start_time =time.time()
    thread1.start()
    thread2.start()
    # thread2.join()
    print("time:{}".format(time.time() - start_time))

 

(zabbixweb) D:\data\python\zabbixapiweb\project\project>d:/data/python/environment/zabbixweb/Scripts/python.exe d:/data/python/zabbixapiweb/project/project/test13.py     
start_func_one
start_func_two
time:0.0009694099426269531
end_func_one
end_func_two
    thread1.setDaemon(False)
    thread2.setDaemon(True)
import threading
import time
def func_one():
    print("start_func_one")
    time.sleep(2)
    print("end_func_one")

def func_two():
    print("start_func_two")
    time.sleep(4)
    print("end_func_two")

if __name__ == "__main__":
    thread1 = threading.Thread(target=func_one)
    thread2 = threading.Thread(target=func_two)
    #把子线程设置成守护线程(表示该线程是不重要的,进程退出时不需要等待这个线程执行完成。
    #这样做的意义在于:避免子线程无限死循环,导致退不出程序,也就是避免传说中的孤儿进程。),
    #即主线程终止时,守护也会终止,不管子线程运行到哪一步。 True表示置该线程为守护线程.
    thread1.setDaemon(False)
    thread2.setDaemon(True)
    start_time =time.time()
    thread1.start()
    thread2.start()
    # thread2.join()
    print("time:{}".format(time.time() - start_time))

 

(zabbixweb) D:\data\python\zabbixapiweb\project\project>d:/data/python/environment/zabbixweb/Scripts/python.exe d:/data/python/zabbixapiweb/project/project/test13.py     
start_func_one
start_func_two
time:0.0009641647338867188
end_func_one
    #join所完成的工作就是线程同步,即主线程任务结束之后,进入阻塞状态,
    #一直等待该线程执行结束后,主线程再终止。
    thread1.join()
import threading
import time
def func_one():
    print("start_func_one")
    time.sleep(2)
    print("end_func_one")

def func_two():
    print("start_func_two")
    time.sleep(4)
    print("end_func_two")

if __name__ == "__main__":
    thread1 = threading.Thread(target=func_one)
    thread2 = threading.Thread(target=func_two)
    #把子线程设置成守护线程(表示该线程是不重要的,进程退出时不需要等待这个线程执行完成。
    #这样做的意义在于:避免子线程无限死循环,导致退不出程序,也就是避免传说中的孤儿进程。),
    #即主线程终止时,守护也会终止,不管子线程运行到哪一步。 True表示置该线程为守护线程.
    # thread1.setDaemon(False)
    # thread2.setDaemon(True)
    start_time =time.time()
    thread1.start()
    thread2.start()
    #join所完成的工作就是线程同步,即主线程任务结束之后,进入阻塞状态,
    #一直等待该线程执行结束后,主线程再终止。
    thread1.join()
    print("time:{}".format(time.time() - start_time))
(zabbixweb) D:\data\python\zabbixapiweb\project\project>d:/data/python/environment/zabbixweb/Scripts/python.exe d:/data/python/zabbixapiweb/project/project/test13.py     
start_func_one
start_func_two
end_func_one
time:2.0020813941955566
end_func_two
thread2.join()
import threading
import time
def func_one():
    print("start_func_one")
    time.sleep(2)
    print("end_func_one")

def func_two():
    print("start_func_two")
    time.sleep(4)
    print("end_func_two")

if __name__ == "__main__":
    thread1 = threading.Thread(target=func_one)
    thread2 = threading.Thread(target=func_two)
    #把子线程设置成守护线程(表示该线程是不重要的,进程退出时不需要等待这个线程执行完成。
    #这样做的意义在于:避免子线程无限死循环,导致退不出程序,也就是避免传说中的孤儿进程。),
    #即主线程终止时,守护也会终止,不管子线程运行到哪一步。 True表示置该线程为守护线程.
    # thread1.setDaemon(False)
    # thread2.setDaemon(True)
    start_time =time.time()
    thread1.start()
    thread2.start()
    #join所完成的工作就是线程同步,即主线程任务结束之后,进入阻塞状态,
    #一直等待该线程执行结束后,主线程再终止。
    thread2.join()
    print("time:{}".format(time.time() - start_time))
(zabbixweb) D:\data\python\zabbixapiweb\project\project>d:/data/python/environment/zabbixweb/Scripts/python.exe d:/data/python/zabbixapiweb/project/project/test13.py     
start_func_one
start_func_two
end_func_one
end_func_two
time:4.001985549926758

 

import time,threading
"""通过共享变量实现线程间通信坏处是变量通信不安全,因为不能控制线程间执行顺序共享变量多了,也不好维护。
    """
detail_list = []


def get_detail_html():
    global detail_list
    print(detail_list)
    print("detail_list_len",len(detail_list))
    print("start get detail html")
    time.sleep(4)
    print("end get detail html")


def get_detail_url():
    global detail_list
    print("start get detail url")
    for x in range(1000):
        detail_list.append("http://www.qq.com/{id}".format(id=x))
    print("end get detail url")

if __name__ == "__main__":
    thread1 = threading.Thread(target=get_detail_url)
    thread2 = threading.Thread(target=get_detail_html)
    #把子线程设置成守护线程(表示该线程是不重要的,进程退出时不需要等待这个线程执行完成。
    #这样做的意义在于:避免子线程无限死循环,导致退不出程序,也就是避免传说中的孤儿进程。),
    #即主进程终止时,守护也会终止,不管子线程运行到哪一步。 True表示置该线程为守护线程.
    # thread1.setDaemon(False)
    # thread2.setDaemon(True)
    start_time =time.time()
    thread1.start()
    thread2.start()
    #join所完成的工作就是线程同步,即主线程任务结束之后,进入阻塞状态,
    #一直等待该线程执行结束后,主线程再终止。
    # thread2.join()
    # print("time:{}".format(time.time() - start_time))

 

posted @ 2021-02-21 22:20  爬行的龟  阅读(72)  评论(0编辑  收藏  举报
如有错误,欢迎指正 邮箱656521736@qq.com