python-- thread lock queue

# Thread-safe data processing - Genlock

Import Time
 DEF Sub ():
     , Ltd. Free Join NUM 

    Print ( " the ok " ) 
    lock.acquire () # get the lock ---> only one thread is executed, the cpu is not allowed to switch, you must complete the implementation of this thread 
    
    TRMP = NUM 
    Time. SLEEP ( 0.01) # ## serial processing 
    NUM = TRMP. 1- Print (NUM) 
    lock.release () # to release the lock 
NUM = 100 Import Threading 
L = [] 
lock = of threading.Lock () # thread-locking for I in Range (100 ): 
    T
    







=threading.Thread(target=sub)
    l.append(t)
    t.start()

for t in l:
    t.join()

print(num)

# Recursive lock



import threading,time

#递归锁

class Mythread(threading.Thread):

def actionA(self):
R_LOCK.acquire()#count=1
print(self.name,"gotA",time.ctime())
time.sleep(2)

R_LOCK.acquire()#count=2
print(self.name, "gotB", time.ctime())
time.sleep(1)

R_LOCK.release()#count=1
R_LOCK.release()#count=0


def actionB(self):
R_LOCK.acquire()
print(self.name, "gotB", time.ctime())
time.sleep(2)

R_LOCK.acquire()
print(self.name, "gotA", time.ctime())
time.sleep(1)

R_LOCK.release()
R_LOCK.release()


def run(self):
self.actionA()
self.actionB()


if __name__=="__main__":
# A=threading.Lock()
# B=threading.Lock()
R_LOCK=threading.RLock()#递归锁

l=[]
for i in range(5):
t=Mythread()
t.start()
l.append(t)

for i in l:
i.join()

print("ending")

 

# Queue, inter-thread data security

Import Queue # thread queue 

# default FIFO -> FIFO queue to address security thread, thread communication 

Q = Queue.Queue (. 3) # store parameter data limits 
q.put (12 is ) 
q.put ( " 123 " ) 
q.put ({ " name " : " Alex " }) # into the data, the data is full blocking 
Print (q.qsize ()) # queue storage size 
Print (q.empty ()) # is empty 
Print ( q.full ()) # is full 

# q.put (22 is, False) when the error exceeds put parameters stored in the parameter specifies 

the while . 1 : 
    Dataq.get = () # get the data, the data is empty, waiting, blocking 
    Print (the Data)
     Print ( " ---------------- " ) 




# --- last-out, LIFO 

# Import Queue 
# 
# Q = queue.LifoQueue () 
# q.put (12 is) 
# q.put ( "Hello") 
# q.put ({ "name": "Yuan"}) 
# 
# the while True : 
#      Data = q.get () 
#      Print (Data) 
#      Print ( "-------------------") 


# numeric priority 
# Import Queue 
# 
# Q = queue.PriorityQueue () 
# q.put([1,12])
# q.put([2,"hello"])
# q.put([4,{"name":"yuan"}])
#
# while True:
#     data=q.get()
#     print(data[1])
#     print("-------------------")
#

 

# Producer-consumer model

Import Time, Random
 Import Queue, Threading 

Q = Queue.Queue () # threads share global queue Q 

DEF Producer (name): 
    COUNT = 0
     the while COUNT <10 :
         Print ( " Making " )
         # the time.sleep (. 5) 
        Q. PUT (COUNT)
         Print ( " Producer Product% S% S has Baozi " % (name, COUNT)) 
        COUNT +. 1 =
         # q.task_done () # queue tells the transmission data has been acquired or 
        q.join ()
         Print ( "OK " ) 

DEF Consumer (name): 
    COUNT = 0
     the while COUNT <10 : 
            the time.sleep (random.randrange ( . 4 ))
             Print ( " waitting " )
         # IF Not q.empty (): 
        #      q.join () # q.join q.task_done acceptance signal if there is no transmission, join the blocking 
            Data = q.get () 
            the time.sleep ( . 4 ) 
            q.task_done () 
            # Print (Data) 
            Print ( " Consumer% S% S has EAT Baozi " %(name,data))
        # else:
        #     print("包子不够了")
            count +=1

p1=threading.Thread(target=Producer,args=('A军',))
p2=threading.Thread(target=Consumer,args=("B君",))
c3=threading.Thread(target=Consumer,args=("C君",))
c4=threading.Thread(target=Consumer,args=("D君",))


p1.start()
p2.start()
c3.start()
c4.start()

 

Guess you like

Origin www.cnblogs.com/tangcode/p/11711523.html