一、查看正在运行的线程、主线程等待子线程先结束
# 01 -多任务-查看线程数 from threading import * import time def test1(): for i in range(5): print("-----test1---%d---"%i) time.sleep(1) def test2(): for i in range(5): print("-----test2---%d---"%i) time.sleep(1) def main(): t1 = Thread(target = test1) t2 = Thread(target = test2) t1.start() t2.start() while True: print(enumerate()) if len(enumerate())<=1: break time.sleep(1) if __name__ == "__main__": main()
1、子线程在调用start后开始运行,在执行完函数代码后结束。
2、线程执行的顺序不确定的。
3、主线程最后结束。
二、通过继承Thread类完成创建线程
# 通过继承Thread类完成创建线程 import threading import time class MyThread(threading.Thread): # 自定义类必须Thread类 def run(self): # 类中必须有run方法 for i in range(3): time.sleep(3) msg = "i'm"+self.name +'@'+str(i) print(msg) if __name__ =="__main__": t = MyThread() t.start() #自动调用run方法
三、多线程共享全局变量
#在一个函数中,对全局变量进行修改的时候到了底是否需要使用global进行说明要看是否对全局变量的执行指向进行了修改,如果修改了执行,即让全局变量指向了一个新的地方,那么必须使用global,如果,仅仅是修改了指向的空间中的数据,此时不用必须使用global。 num = 100 nums = [11,22] def test(): global num num += 100 def test2(): nums.append(33) print(num) print(nums) test() test2() print(num) print(nums)
import threading import time g_num = 100 def test1(): global g_num g_num += 1 print("----in test1 g_num = %d----"%g_num) def test2(): print("----in test2 g_num = %d----"%g_num) def main(): t1 = threading.Thread(target = test1) t2 = threading.Thread(target = test2) t1.start() time.sleep(1) t2.start() time.sleep(1) print("----in main Thread g_num = %d---"%g_num) if __name__ == "__main__": main()
import threading import time def test1(temp): temp.append(33) print("----in test1 g_nums = %s----"%str(temp)) def test2(temp): print("----in test2 g_nums = %s----"%str(temp)) g_nums = [11,22] def main(): #target指定将来这个线程去哪个函数执行代码 #args指定将来调用函数的时候,传递什么数据过去 t1 = threading.Thread(target = test1,args=(g_nums,)) t2 = threading.Thread(target = test2,args=(g_nums,)) t1.start() time.sleep(1) t2.start() time.sleep(1) print("----in main Thread g_nums = %s---"%str(g_nums,)) if __name__ == "__main__": main()
import threading import time g_num = 0 def test1(num): global g_num for i in range(num): g_num += 1 print("----in test1 g_num = %d----"%g_num) def test2(num): global g_num for i in range(num): g_num += 1 print("----in test2 g_num = %d----"%g_num) def main(): t1 = threading.Thread(target = test1,args=(1000000,)) t2 = threading.Thread(target = test2,args=(1000000,)) t1.start() t2.start() time.sleep(5) print("----in main Thread g_num = %d---"%g_num) if __name__ == "__main__": main() 执行结果: ----in test1 g_num = 1330332---- ----in test2 g_num = 1314201---- ----in main Thread g_num = 1314201--- 根本原因:g_num += 1 分三步:1、获取g_num值 2、把获取的g_num的值加1 3、把第二步的结果存到g_num中
#使用互斥锁解决资源竞争的问题 import threading import time g_num = 0 def test1(num): global g_num #上锁,如果之前没有被上锁,那么此时上锁成功 #如在上锁之前,已经被锁上,那么此时会堵塞在这里,知道这个锁被解开 mutex.acquire() for i in range(num): g_num += 1 mutex.release() print("----in test1 g_num = %d----"%g_num) def test2(num): global g_num mutex.acquire() for i in range(num): g_num += 1 mutex.release() print("----in test2 g_num = %d----"%g_num) #创建一个互斥锁,默认是没有上锁的 mutex = threading.Lock() def main(): t1 = threading.Thread(target = test1,args=(1000000,)) t2 = threading.Thread(target = test2,args=(1000000,)) t1.start() t2.start() time.sleep(5) print("----in main Thread g_num = %d---"%g_num) if __name__ == "__main__": main()
#使用互斥锁解决资源竞争的问题 import threading import time g_num = 0 def test1(num): global g_num #上锁,如果之前没有被上锁,那么此时上锁成功 #如在上锁之前,已经被锁上,那么此时会堵塞在这里,知道这个锁被解开 for i in range(num): mutex.acquire() g_num += 1 mutex.release() print("----in test1 g_num = %d----"%g_num) def test2(num): global g_num for i in range(num): mutex.acquire() g_num += 1 mutex.release() print("----in test2 g_num = %d----"%g_num) #创建一个互斥锁,默认是没有上锁的 mutex = threading.Lock() def main(): t1 = threading.Thread(target = test1,args=(1000000,)) t2 = threading.Thread(target = test2,args=(1000000,)) t1.start() t2.start() time.sleep(5) print("----in main Thread g_num = %d---"%g_num) if __name__ == "__main__": main()
#多线程udp聊天器 import socket import threading def recv_msg(udp_socket): while True: recv_date = udp_socket.recv(1024) print("接收到的信息是:%s"%recv_date.decode("utf-8")) def send_msg(udp_socket,ip,port): while True: msg = input("请输入要发送的信息:") udp_socket.sendto(msg.encode("utf-8"),(ip,port)) def main(): udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) udp_socket.bind(("",7788)) dest_ip = input("请输入对方的ip地址:") dest_port = int(input("请输入对方的端口号:")) t_recv = threading.Thread(target = recv_msg, args = (udp_socket,)) t_send = threading.Thread(target = send_msg, args = (udp_socket,dest_ip,dest_port)) t_recv.start() t_send.start() if __name__ =="__main__": main()
浙公网安备 33010602011771号