python basis of a day40 condition timer queue thread pool

# Condition
# lock
# Acquire Release
# beginning of a condition is created by default have a state False
# False status affects the wait has been in a wait state
# notify (int data type) made key

notify and wait required between acquire release

wait () is waiting for a key, written in the code between the key to acquire and wait without limitation, to lock the code must be placed behind the wait

The key is not made out yet, it can only be used once

from threading import Thread,Condition
def func(con,i):
    con.acquire()
    con.wait() # 等钥匙
    print('在第%s个循环里'%i)
    con.release()
con = Condition()
for i in range(10):
    Thread(target=func,args = (con,i)).start()
while True:
    num = int(input('>>>'))
    con.acquire()
    con.notify(num)  # 造钥匙
    con.release()

Timer

 

Import Time
 from Threading Import the Timer
 DEF FUNC ():
     Print ( ' Time Synchronization ' )    # 1-3 

the while True: 
    T = the Timer (. 5, FUNC) .start ()    # nonblocking 
    time.sleep (5)

queue:

# Queue 
Import Queue 

Q = Queue.Queue ()   # queue FIFO 
# q.put () 
# q.get () 
# q.put_nowait () 
# q.get_nowait () 

# Q = queue.LifoQueue () # Stack advanced after 
# q.put (. 1) 
# q.put (2) 
# q.put (. 3) 
# Print (q.get ()) 
# Print (q.get ()) 

    Q = queue.PriorityQueue ()   # priority queue 
    q.put ((20 is, ' A ' )) 
    q.put (( 10, ' B '  ))
    q.put ((30,'c'))
    q.put((-5,'d'))
    q.put((1,'?'))
    print(q.get())

Thread Pool:

Import Time
 from concurrent.futures Import the ThreadPoolExecutor
 DEF FUNC (n-): 
    the time.sleep ( 2 )
     Print (n-)
     return n-* n- 

DEF call_back (m):
     Print ( ' result S% ' % m.result ()) 

TPool is ThreadPoolExecutor = (max_workers = 5)    #   default cpu do not exceed the number of 5 * 
for i in   the Range (20 ): 
    tpool.submit (FUNC, i) .add_done_callback (call_back) 


# tpool.map (FUNC, the Range (20)) # can not get the return value 
# t_lst = []
# for i in  range(20):
#     t = tpool.submit(func,i)
#     t_lst.append(t)
# tpool.shutdown()  # close+join    #
# print('主线程')
# for t in t_lst:print('***',t.result())

# ftp
# 并发编程

 

Guess you like

Origin www.cnblogs.com/wang-tan/p/11449230.html