1.信号量

import threading,time

class myThread(threading.Thread):
def run(self): if semaphore.acquire():
print(self.name)
time.sleep(3)
semaphore.release() if __name__=="__main__":
semaphore=threading.Semaphore() thrs=[]
for i in range(100):
thrs.append(myThread())
for t in thrs:
t.start()

2.同步对象

import threading,time
class Boss(threading.Thread): def run(self):
print("BOSS:今晚大家都要加班到22:00。")
print(event.isSet())# False
event.set()
time.sleep(5)
print("BOSS:<22:00>可以下班了。")
print(event.isSet())
event.set() class Worker(threading.Thread):
def run(self): event.wait()# 一旦event被设定,等同于pass print("Worker:哎……命苦啊!")
time.sleep(1)
event.clear()
event.wait()
print("Worker:OhYeah!") if __name__=="__main__":
event=threading.Event() threads=[]
for i in range(5):
threads.append(Worker())
threads.append(Boss())
for t in threads:
t.start()
for t in threads:
t.join() print("ending.....")

3.生产者消费者模型

# import time,random
# import queue,threading
#
# q = queue.Queue()
#
# def Producer(name):
# count = 0
# while count <10:
# print("making........")
# time.sleep(5)
# q.put(count)
# print('Producer %s has produced %s baozi..' %(name, count))
# count +=1
# #q.task_done()
# q.join()
# print("ok......") # def Consumer(name):
# count = 0
# while count <10:
# time.sleep(random.randrange(4))
# # if not q.empty():
# # print("waiting.....")
# #q.join()
# data = q.get()
# print("eating....")
# time.sleep(4)
#
# q.task_done()
# #print(data)
# print('\033[32;1mConsumer %s has eat %s baozi...\033[0m' %(name, data))
# # else:
# # print("-----no baozi anymore----")
# count +=1
#
# p1 = threading.Thread(target=Producer, args=('A君',))
# c1 = threading.Thread(target=Consumer, args=('B君',))
# c2 = threading.Thread(target=Consumer, args=('C君',))
# c3 = threading.Thread(target=Consumer, args=('D君',))
#
# p1.start()
# c1.start()
# c2.start()
# c3.start()

4.递归锁

import  threading
import time class MyThread(threading.Thread): def actionA(self): r_lcok.acquire() #count=1
print(self.name,"gotA",time.ctime())
time.sleep(2)
r_lcok.acquire() #count=2 print(self.name, "gotB", time.ctime())
time.sleep(1) r_lcok.release() #count=1
r_lcok.release() #count=0 def actionB(self): r_lcok.acquire()
print(self.name, "gotB", time.ctime())
time.sleep(2) r_lcok.acquire()
print(self.name, "gotA", time.ctime())
time.sleep(1) r_lcok.release()
r_lcok.release() def run(self): self.actionA()
self.actionB() if __name__ == '__main__': # A=threading.Lock()
# B=threading.Lock() r_lcok=threading.RLock()
L=[] for i in range(5):
t=MyThread()
t.start()
L.append(t) for i in L:
i.join() print("ending....")

5.进程调用

# from multiprocessing import Process
# import time
#
#
# def f(name):
# time.sleep(1)
# print('hello', name,time.ctime())
#
# if __name__ == '__main__':
# p_list=[]
# for i in range(3):
#
# p = Process(target=f, args=('alvin',))
# p_list.append(p)
# p.start()
#
# for i in p_list:
# i.join()
# print('end') from multiprocessing import Process
import time # class MyProcess(Process):
#
# # def __init__(self):
# # super(MyProcess, self).__init__()
# # #self.name = name
#
# def run(self):
# time.sleep(1)
# print ('hello', self.name,time.ctime())
# # if __name__ == '__main__':
# p_list=[]
#
#
# for i in range(3):
# p = MyProcess()
# p.daemon=True
# p.start()
# p_list.append(p)
#
# # for p in p_list:
# # p.join()
#
# print('end') # from multiprocessing import Process
# import os
# import time
#
#
# def info(title):
# print("title:", title)
# print('parent process:', os.getppid())
# print('process id:', os.getpid())
#
# def f(name):
#
# info('function f')
# print('hello', name)
from multiprocessing import Process
import os
import time
def info(title):
print("title",title)
print("parent process:",os.getppid())
print("process id:",os.getpid()) def f(name):
info('function f')
print("hello",name) if __name__ == '__main__':
info('main process line')
time.sleep(1)
print("-------")
p = Process(target=info("yuan"))
p.start()
p.join()
# if __name__ == '__main__':
#
# info('main process line')
#
# time.sleep(1)
# print("------------------")
# p = Process(target=info, args=('yuan',))
# p.start()
# p.join()
05-15 04:11