标签:sleep 创建 elf nbsp 数据 保存 ges 方式 import
###########使用线程完成多任务################
from threading import Thread import time #1. 如果多个线程执行的都是同一个函数的话,各自之间不会有影响,各是个的 def test(): print("----昨晚喝多了,下次少喝点---") time.sleep(1) for i in range(5): t = Thread(target=test) t.start()
#############使用类的方式创建线程完成任务###########
import threading import time class MyThread(threading.Thread): def run(self): for i in range(3): time.sleep(1) msg = "I‘m "+self.name+‘ @ ‘+str(i) #name属性中保存的是当前线程的名字 print(msg) if __name__ == ‘__main__‘: t = MyThread() t.start()
执行结果:
[root@master process]# python3 09-thread.py I‘m Thread-1 @ 0 I‘m Thread-1 @ 1 I‘m Thread-1 @ 2
###############线程之间共享全局变量#########
from threading import Thread import time #线程之间共享全局变量 g_num = 100 def work1(): global g_num for i in range(3): g_num += 1 print("----in work1, g_num is %d---"%g_num) def work2(): global g_num print("----in work2, g_num is %d---"%g_num) print("---线程创建之前g_num is %d---"%g_num) t1 = Thread(target=work1) t1.start() #延时一会,保证t1线程中的事情做完 time.sleep(1) t2 = Thread(target=work2) t2.start()
############线程之间共享全局变量带来的问题############
from threading import Thread import time g_num = 0 def test1(): global g_num for i in range(1000000): g_num += 1 print("---test1---g_num=%d"%g_num) def test2(): global g_num for i in range(1000000): g_num += 1 print("---test2---g_num=%d"%g_num) p1 = Thread(target=test1) p1.start() #time.sleep(3) #取消屏蔽之后 再次运行程序,结果会不一样,,,为啥呢? p2 = Thread(target=test2) p2.start() print("---g_num=%d---"%g_num)
############把列表当做参数传递给线程############
from threading import Thread import time def work1(nums): nums.append(44) print("----in work1---",nums) def work2(nums): #延时一会,保证t1线程中的事情做完 time.sleep(1) print("----in work2---",nums) g_nums = [11,22,33] t1 = Thread(target=work1, args=(g_nums,)) t1.start() t2 = Thread(target=work2, args=(g_nums,)) t2.start()
###############避免多线程对共享数据出错的方式###########
from threading import Thread import time g_num = 0 g_flag = 1 def test1(): global g_num global g_flag if g_flag == 1: for i in range(1000000): g_num += 1 g_flag = 0 print("---test1---g_num=%d"%g_num) def test2(): global g_num #轮询 while True: if g_flag != 1: for i in range(1000000): g_num += 1 break print("---test2---g_num=%d"%g_num) p1 = Thread(target=test1) p1.start() #time.sleep(3) #取消屏蔽之后 再次运行程序,结果会不一样,,,为啥呢? p2 = Thread(target=test2) p2.start() print("---g_num=%d---"%g_num)
##############使用互斥锁解决共享数据出错问题##################
代码例子:
from threading import Thread, Lock import time g_num = 0 def test1(): global g_num #这个线程和test2线程都在抢着 对这个锁 进行上锁,如果有1方成功的上锁,那么导致另外 #一方会堵塞(一直等待)到这个锁被解开为止 mutex.acquire() for i in range(1000000): g_num += 1 mutex.release()#用来对mutex指向的这个锁 进行解锁,,,只要开了锁,那么接下来会让所有因为 #这个锁 被上了锁 而堵塞的线程 进行抢着上锁 print("---test1---g_num=%d"%g_num) def test2(): global g_num mutex.acquire() for i in range(1000000): g_num += 1 mutex.release() print("---test2---g_num=%d"%g_num) #创建一把互斥锁,这个锁默认是没有上锁的 mutex = Lock() p1 = Thread(target=test1) p1.start() #time.sleep(3) #取消屏蔽之后 再次运行程序,结果会不一样,,,为啥呢? p2 = Thread(target=test2) p2.start() print("---g_num=%d---"%g_num)
#############多线程使用非共享变量################
from threading import Thread import threading import time def test1(): #注意: # 1. 全局变量在多个线程中 共享,为了保证正确运行需要锁 # 2. 非全局变量在每个线程中 各有一份,不会共享,当然了不需要加锁 name = threading.current_thread().name print("----thread name is %s ----"%name) g_num = 100 if name == "Thread-1": g_num += 1 else: time.sleep(2) print("--thread is %s----g_num=%d"%(name,g_num)) #def test2(): # time.sleep(1) # g_num = 100 # print("---test2---g_num=%d"%g_num) p1 = Thread(target=test1) p1.start() p2 = Thread(target=test1) p2.start()
执行结果:
###################同步#################
标签:sleep 创建 elf nbsp 数据 保存 ges 方式 import
原文地址:http://www.cnblogs.com/shanhua-fu/p/7754975.html