11. The multi-threaded, multi-process and thread pool program

1.1. Thread Synchronization Lock and Rlock

(1)Lock

  • Locks affect performance
  • With locks deadlock
import threading
from threading import Lock

total = 0
lock = Lock()

def add():
    global total
    global local
    for i in range(100000):
        lock.acquire()
        # lock.acquire()   #如果再加把锁会产生死锁
        total += 1
        lock.release()

def desc():
    global total
    global local
    for i in range(100000):
        lock.acquire()     #获取锁
        total -= 1
        lock.release()     #释放锁

thread1 = threading.Thread(target=add)
thread2 = threading.Thread(target=desc)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(total)   #0

(2)RLock

RLock: the number of times the same thread inside, you can acquire continuously called multiple times, must pay attention to acquire and release of equal

Import Threading
 from Threading Import Lock, RLock 

Total = 0 
Lock = RLock () 

DEF the Add ():
     , Ltd. Free Join Total
     , Ltd. Free Join local
     for i in the Range (100000 ):
         # use RLock in the same thread inside, you can call acquire many times, does not produce deadlock 
        lock.acquire () 
        lock.acquire () 
        Total + =. 1
         # is equal to the number of times of release and acquire the 
        lock.release () 
        lock.release () 

DEF desc ():
     Global Total
    global local
    for i in range(100000):
        lock.acquire()     #获取锁
        total -= 1
        lock.release()     #释放锁

thread1 = threading.Thread(target=add)
thread2 = threading.Thread(target=desc)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
print(total)   #0

1.2 Thread Synchronization - condition 

Use condition Analog Dialogue

Import Threading
 from Threading Import for condition Condition 

 # by condition, read and complete synergy 
class XiaoAi (of the threading.Thread):
     DEF  the __init__ (Self, cond): 
        . Super () the __init__ (name = ' small love ' ) 
        self.cond = cond 

    DEF RUN (Self): 
        with self.cond: 
            # wait 
            self.cond.wait ()
             Print ( " {}: in the " .format (the self.name))
             # notification 
            self.cond.notify ()

            self.cond.wait()
            print("{} : 好啊".format(self.name))
            self.cond.notify()

            self.cond.wait()
            print("{} : 君住长江尾".format(self.name))
            self.cond.notify()

class TianMao(threading.Thread):
    def __init__(self,cond):
        super().__init__(name="天猫精灵")
        self.cond = cond

    def run(self):
        with self.cond:
            print("{}: Small students love " .format (self.name)) 
            self.cond.notify () 
            self.cond.wait () 

            Print ( " {}: Let's poetry to it " .format (self.name)) 
            Self .cond.notify () 
            self.cond.wait () 

            Print ( " {}: I am in the Yangtze River head " .format (self.name)) 
            self.cond.notify () 
            self.cond.wait () 

IF  __name__ == ' __main__ ' : 
    cond = threading.Condition () 
    xiaoai = XiaoAi (cond) 
    tianmao =TianMao (cond) 

    xiaoai.start () 
    tianmao.start ()

result:

 

1.3 Thread Synchronization - Semaphore 

Control the number of concurrent threads

# Samaphore is a number of lock control proceeds 

Import Threading
 Import Time 

class htmlSpider (of the threading.Thread):
     DEF  the __init__ (Self, URL, SEM): 
        . Super () the __init__ () 
        self.url = URL 
        self.sem = SEM 

    DEF RUN (Self): 
        the time.sleep ( 2 )
         Print ( " ! GOT HTML text Success " ) 
        self.sem.release ()    # release lock 

class UrlProducer (threading.Thread):
     DEF  __init__(Self, sem): 
        Super (). __init__ () 
        self.sem = sem
     DEF RUN (Self):
         for i in the Range (20 ): 
            self.sem.acquire ()     # locked 
            html_htread = htmlSpider ( " baidu.com / {} " .format (I), self.sem) 
            html_htread.start () 

IF  the __name__ == ' __main__ ' :
     # control of the number of concurrent threads. 3 
    SEM = threading.Semaphore (. 3 ) 
    url_producer =  UrlProducer (SEM)
    url_producer.start ()

11.4.ThreadPoolExecutor thread pool

Thread Pool

from concurrent.futures Import ThreadPoolExecutor, as_completed
 Import Time 

# Why should the thread pool 
# main thread can acquire a certain thread of a state or status of a task, as well as the return value of 
# when a thread is completed, the main thread immediately know 
# Futures allows multi-threaded and multi-process coding interfaces consistent 

DEF get_html (Times): 
    the time.sleep (Times) 
    Print ( " GET {} Page Success " .format (Times))
     return Times 

Executor = ThreadPoolExecutor (max_workers = 2 ) 

# by submit submitted to the function performed by the thread pool, sumbit immediately return 
task1 = executor.submit (get_html, (3))     # function and parameter 

#done method for determining whether a task is completed 
Print (task1.done ())       # False 
the time.sleep (4 )
 Print (task1.done ())       # True 
# the Result ways to view the structure of task execution function 
Print (task1.result ())     # 3

Get a task completed return as_completed

from concurrent.futures Import ThreadPoolExecutor, as_completed
 Import Time 

# Why should the thread pool 
# main thread can acquire a certain thread of a state or status of a task, as well as the return value of 
# when a thread is completed, the main thread immediately know 
# Futures allows multi-threaded and multi-process coding interfaces consistent 

# DEF get_html (Times): 
#      the time.sleep (Times) 
#      Print ( "GET {} Page Success" .format (Times)) 
#      return Times 
# 
# Executor = ThreadPoolExecutor (max_workers = 2) 
# 
# # submit submitted by the thread pool to execute the function, sumbit return immediately 
# Task1 = executor.submit (get_html, (. 3)) and the function parameters # 
# 
##done method for determining whether a task is completed 
# Print (task1.done ()) #False 
# the time.sleep (4) 
# Print (task1.done ()) #True 
# #resultThe way to view the structure function of task execution 
# Print (task1.result ()) #. 3 

DEF get_html (Times): 
    the time.sleep (Times) 
    Print ( " GET Page {} Success " .format (Times))
     return Times 

Executor = the ThreadPoolExecutor (max_workers = 2 ) 

# Get has returned to the task of successfully 
urls = [3,2,4 ] 
all_task = [executor.submit (get_html, (url)) for url in urls]

for Future in as_completed (all_task): 
    the Data = future.result ()
     Print (the Data)    # return has been successful task function

11.5 interprocess communication - Queue

Queue

import time

from multiprocessing import Process, Queue

def producer(queue):
    queue.put("a")
    time.sleep(2)

def consumer(queue):
    time.sleep(2)
    data = queue.get()
    print(data)

if __name__ == '__main__':
    queue = Queue(10)
    my_producer = Process(target=producer, args=(queue,))
    my_consumer = Process(target=consumer, args=(queue,))

    my_producer.start()
    my_consumer.start()
    my_producer.join()
    my_consumer.join()

11.6 interprocess communication - Manager

Manger

Import Time 

from multiprocessing Import Process, Queue, Manager, Pool 

DEF Producer (Queue): 
    queue.put ( " A " ) 
    the time.sleep ( 2 ) 

DEF Consumer (Queue): 
    the time.sleep ( 2 ) 
    Data = queue.get ( )
     Print (Data) 

IF  the __name__ == ' __main__ ' : 
    # inter-process communication in the pool requires the use of the queue manger 
    Queue Manager = () Queue (10. ) 
    pool = pool (2)    # Create process pool

    pool.apply_async(producer, args=(queue, ))
    pool.apply_async(consumer, args=(queue, ))

    pool.close()
    pool.join()

11.7 Inter-Process Communication - Pipe

pipe interprocess communication (only between two processes)

#Pipe进程间通信
from multiprocessing import Process, Pipe

def producer(pipe):
    pipe.send("derek")

def consumer(pipe):
    print(pipe.recv())

if __name__ == '__main__':
    receive_pipe, send_pipe = Pipe()
    my_producer = Process(target=producer, args=(send_pipe, ))
    my_consumer = Process(target=consumer, args=(receive_pipe, ))

    my_producer.start()
    my_consumer.start()
    my_producer.join()
    my_producer.join()

 

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11392029.html