Python Way (Fourth fifteen) thread Event event, condition Condition, timer Timer, thread queue

First, the event Event

Event (events): event handling mechanism: Global defines a built-mark Flag, Flag if the value is False, then when the program is executed event.wait method will block if Flag is True, then the method will be when event.wait no longer blocked.

 

Event is actually a simplified version of the Condition. Event not locked, it can not synchronize the thread into the blocked state.

Event()

  • set (): The flag is set to True, and notify all the threads in a wait state blocked the recovery operation.

  • clear (): The flag is set to False.

  • wait (timeout): If the flag is True will be returned immediately, otherwise blocked blocked thread to wait, wait for the other thread calls set ().

  • isSet (): Gets the built-in flags state, returns True or False.

 

A key feature is that each thread is a separate thread running and an unpredictable state. If other threads in the program need to determine their next action by judging the state of a thread, then thread synchronization problem will become very difficult. To solve these problems, we need to use the Event object threading library. Object contains a set of threads by a signal flag that allows a thread to wait for the occurrence of certain events.

Procedure: After the initial case, the signal Event object flag is set to false. If there is a thread to wait for an Event object, and the sign of the Event object is false, then the thread will be blocked until the flag has been true. If one thread will signal an Event object flag is set to true, it wakes up all the threads waiting for this Event object. If a thread is waiting for a true Event it has been set as the object, then it will ignore the event, continue.

example

scenes to be used:

There are multiple worker threads try to link MySQL, we want to make sure MySQL before linking normal service only for those who work thread to connect to the MySQL server, if the connection is not successful, we will try to reconnect. Then we can use threading.Event mechanism to coordinate the operation of each worker thread connection

Threading the Event Import from, the Thread 
Import Time 
Import Random 
DEF connect_db (E): 
    the time.sleep (the random.randint (0,3)) 
    e.set () 
DEF check_web (E): 
    COUNT =. 1 
    the time.sleep ( 1) 
    the while COUNT <4: 
        e.wait (0.5) # state False, you can just wait for 0.5s ended 
        IF e.is_set (): 
            "!% s of times a database link success" Print (COUNT%) 
            BREAK 
        the else : 
            ( "!% s of secondary link database failure" COUNT%) Print 
            COUNT + =. 1 
    the else: 
        The raise TimeoutError ( "database link timeout") 
E = the Event () 
T2 = the Thread (target = connect_db, args = (E, )) 
t1.start () 
T1 = the Thread (target = check_web, args = (E, ))
t2.start()

  

Second, conditions Condition

Condition can be understood as a senior petty, it offers more advanced than Lock, RLock feature that allows us to control complex thread synchronization issues. threadiong.Condition internally maintains a trivial objects (default is RLock), you can create Condigtion object when the object as a petty argument.

Condition also provided acquire, release method, consistent with its meaning petty acquire, release method, in fact, it simply calls the corresponding method trivial objects inside it.

Condition also provides the following methods (with particular attention: these methods can only be called after the occupation Suo (acquire), otherwise it will report RuntimeError exception.):

 

  • Condition.wait([timeout]):

    The method of releasing the internal wait Suo occupied, while the thread is suspended until a wake-up notification is received or a timeout (timeout parameter, if it is provided). When the thread wakes up and re-occupy Suo, the program will continue execution.

  • Condition.notify():

    Wake up a suspended thread (if there is a pending thread). Note: notify () method does not release the occupied petty.

  • Condition.notify_all() Condition.notifyAll()

    Wake up all pending thread (if there is a pending thread). Note: These methods do not release the occupied petty.

 

example

 



Threading the Thread Import from, for condition Condition 
DEF FUNC (CON, I): 
    con.acquire () 
    con.wait () 
    Print ( "% s of execution thread"% I) 
    con.release () 
CON = for condition Condition () 
for I in Range (10): 
    the Thread (target = FUNC, args = (CON, I)) Start (). 
the while True: 
    NUM = INPUT ( ">>>") Strip (). 
    IF num.isdigit ( ): 
        NUM = int (NUM) 
    the else: BREAK 
    con.acquire () 
    con.notify (NUM) # notice there are several threads can execute 
    con.release () 
# for condition Condition Python provides an object provides synchronization of complex threads stand by. 
Conditions for condition Condition # variable is referred to, in addition to providing Lock similar to acquire and release method, 
# is also provided wait and notify methods. Thread first acquire a condition variable, and then determine a number of conditions. 
# If the wait condition is not satisfied; If the condition is satisfied, some processing conditions change,
# Notify other threads through the notify method, other threads in the wait state after receiving the notice will be re-judged conditions. 
# Keep repeating this process, so as to solve complex synchronization problems.

  


Analysis: Enter a number in the program is running, the number of digital inputs how many threads for loop in the thread wake.

 

Third, a timer Timer

Timer: call a function every certain period of time, if you want to achieve from time to time to call a function, then we would call the function Timer, set the Timer again. Timer Thread is a derived class.

 

Timer sub inherit the Thread class is a subclass of Thread, thread class also has the ability and character threads. This class is used to define how long the execution of a function.

Its example is the ability to delay the implementation of the objective function of the thread, before the actual implementation of the objective function, you can cancel it.

Timer()

The first parameter is transmitted every interval time

After the transfer function function to perform tasks across a number of seconds to perform this function

To the function parameter passing mode args kwargs

 

Timer use the Thread modules, each starts a timer, start a thread

 

 

example

 

​
from threading import Timer
import time
​
​
def add(x, y):
    print(x + y)
​
​
t = Timer(10,add,args=[4,5,])
t.start()
​
time.sleep(2)
t.cancel()
print("===end===")

  


 Analysis: After performing the start method, the Timer object is in the waiting state, it will wait 10 seconds after the execution of the add function. Meanwhile, the waiting period before the implementation of the add function, the main thread using a method cancel the child thread will skip the end of the execution of the function.

 

example


# Timer after a specified n seconds to perform an action 
from the Timer Threading Import 
Import Time 
DEF FUNC (): 
    Print ( "Execution Time Synchronization") 
the while True: 
    the Timer (5, FUNC) .start () # Note that this a thread is asynchronous, first sentence execution, then immediately execute time.sleep (), 
    # timer func executed after waiting five seconds, and time.sleep () at the moment, wait for 5 seconds, re-execute timer () 
    Time .sleep (5)

  

 

 

Fourth, the thread queue

Offers several queues queue blocking module, mainly used to implement the queues thread communication. In the main queue module provides three categories, representing three queue, the main difference is that the inlet queues different queues.

About three queue classes briefly as follows:

  • Queue.qsize (): returns the actual size of the queue, i.e. the several elements contained in the queue.

  • Queue.empty (): whether the queue is empty.

  • Queue.full (): whether the queue is full.

  • Queue.put (item, block = True, timeout = None): put into a queue element. If the queue is already full, and the block parameters is True (blocking), the current thread is blocked, timeout specified blocking time, if the timeout is set to None, represents blocks until the elements of the queue are consumed; if the queue is already full, and block parameter is False (not blocked), directly caused queue.FULL exception.

  • Queue.put_nowait (item): put into a queue element, without blocking. It corresponds to False in the method in a parameter block.

  • Queue.get (item, block = True, timeout = None): Remove the element (element consumption) from the queue. If the queue is full, and the block parameter is True (blocking), the current thread is blocked, the blocking time specified timeout, if the timeout is set to None, represents the block until all elements are placed in a queue; if the queue is already empty, and block parameters is False (not blocked), directly caused queue.EMPTY exception.

  • Queue.get_nowait (item): Remove the element from the queue without blocking. It corresponds to False in the method in a parameter block.

  • Queue.Queue (maxsize = 0) #FIFO, used to define the length of the queue, if it indicates that the queue is less than 1 maxsize infinite length,

  • Queue.LifoQueue (maxsize = 0) #LIFO, if it represents less than 1 maxsize queue of infinite length

  • Before task_done () # implies a task the team has been completed. Thread calls by the consumer queue. Each get () call to get a job, the next task_done () call tells the queue if the current task has been dealt a join () is blocked, it will resume execution (that is, every time all the tasks in the queue are processed a () is called by the task team has put into a corresponding task_done () call).

  • join () # block the calling thread until all tasks in the queue to be disposed of. As long as the data is queued, the number of unfinished tasks will increase. When the consumer thread calls task_done ((mean consumers have made the task and complete the task), the number of unfinished tasks will be reduced. When the number of unfinished tasks down to 0, join () unblocked.

 

Three different classes of queues

q = Queue (maxsize): creates a FIFO (first-in first-out , FIFO) queue. maxsize is the maximum number of entries in the queue to put the money. 
If omitted, or it maxsize parameter is set to 0, the size of the queue will be infinite. q = LifoQueue (maxsize): Create a LIFO (last-in first-out , last in first out) queue (stack). q = PriorityQueue (maxsize): creates a priority queue, wherein the priority according to the item from low to high lined. When such a queue entry should be (priority, data) in the form of tuples, wherein a priority identifying numerals priority.




example

Used in the process queue # Import Queue multiprocessing from 
Import Queue 
# 
# Ql = Queue.Queue () # FIFO 
# q1.get_nowait () # does not block the direct access, if the error takes empty 
# q1.put_nowait (2) # does not block direct deposit, if the deposit is full error 
# q1.get () 
# q1.put (1) 
q2 = queue.LifoQueue () # actually a stack, advanced after 
q2.put ( " a ") 
q2.put (" B ") 
q2.put (" C ") 
Print (q2.get ()) 
q3 = queue.PriorityQueue () # priority queue, priority determination based on extracted object disposed 
q3 .get ((20 is, "A")) 
q3.get ((10, "C")) 
q3.get ((0, "B")) 
q3.get ((- 10, "D")) # digital the smaller the priority taken out 
q3.get ((30, "E")) 
Print (q3.get ())

  


Producer consumer model

import threading
import time
from queue import Queue
​
​
def put_id():
    i = 0
    while True:
        i = i + 1
        print("添加数据", i, id_queue.qsize())
        time.sleep(1)
        id_queue.put(i)
​
​
def get_id(m):
    while True:
        i = id_queue.get()
        print("线程", m, '取值', i)
​
​
if __name__ == "__main__":
    id_queue = Queue(10)
​
    Th1 = threading.Thread(target=put_id, )
    Th2 = threading.Thread(target=get_id, args=(2,))
    Th3 = threading.Thread(target=get_id, args=(3,))
    Th5 = threading.Thread(target=get_id, args=(4,))
    Th4 = threading.Thread(target=get_id, args=(5,))
​
    Th1.start()
    Th2.start()
    Th3.start()
    Th4.start()
    Th5.start()

  

 

 

 

Guess you like

Origin www.cnblogs.com/Nicholas0707/p/11441033.html