Home  >  Article  >  Backend Development  >  Python development--detailed explanation of processes, threads, and coroutines

Python development--detailed explanation of processes, threads, and coroutines

零下一度
零下一度Original
2017-06-27 09:57:521470browse

What is a process?

A program cannot run alone. Only when the program is loaded into memory and the system allocates resources to it can it run. This executed program is called a process. The difference between a program and a process is that a program is a collection of instructions, which is a static description text of the process; a process is an execution activity of the program and is a dynamic concept.

What is a thread?

Thread is the smallest unit that the operating system can perform operation scheduling. It is included in the process and is the actual operating unit in the process. A thread refers to a single sequential control flow in a process. Multiple threads can run concurrently in a process, and each thread performs different tasks in parallel.

What is the difference between process and thread?

Threads share memory space, and the memory of the process is independent.

Threads of the same process can communicate directly, but two processes must communicate with each other through an intermediate agent.

Creating a new thread is very simple. Creating a new process requires cloning its parent process.

A thread can control and operate other threads in the same process, but a process can only operate child processes.

Python GIL (Global Interpreter Lock)

No matter how many threads are opened and how many CPUs there are, python only allows one thread at the same time when executing.

Python threading module

Direct call

  1. import threading,time
  2. def run_num(num):
  3. ## """
  4. Define the function to be run by the thread
  5. ## :param num:
  6. :
    return:
  7. ## """
  8. print("
  9. running on number:%s
    "%num)
    ## time.sleep(3)
  10. ##if
  11. __name__ == '__main__':
  12. # Generate a thread instance t1
  13. ## t1 = threading.Thread(target=run_num,args=(1,) )
  14. # Generate a thread instance t2
  15. t2 = threading.Thread(target =run_num,args=(2,))
  16. # Start thread t1
  17. t1.start()
  18. Start thread t2
  19. ## t2.start()
  20. # Get the thread name
  21. print(t1.getName())
  22. print(t2.getName())
  23. Output:
  24. running on number:1
  25. running on number:2
  26. Thread-1
  27. Thread-2
  28. Inherited calling
  29. import threading,time

  1. class MyThread(threading.Thread):
  2. ## def __init__(self,num :
  3. ## num
  4. # Define the function to be run by each thread. The function name must be run
  5. def run(self):
  6. ## print("
  7. running on number:%s
    "%self.num)
  8. time.sleep(3)
  9. ##if
    __name__ == '__main__':
  10. ## t1 = MyThread(1)
  11. t2 = MyThread(2)
  12. ## t1.start()
  13. t2.start()
  14. Output:
  15. ##running on number :1
  16. running on number:2
  17. ##Join and Daemon
  18. Join The function of Join is to block the main process and cannot execute the program after the join.
  19. In the case of multi-threads and multiple joins, the join methods of each thread are executed sequentially. The execution of the previous thread is completed before the subsequent thread can be executed.
  20. When there are no parameters, wait for the thread to end before executing subsequent programs. After setting the parameters, wait for the time set by the thread and then execute the subsequent main process, regardless of whether the thread ends.
  21. import threading,time

    ##class
      MyThread(threading.Thread):
    1. ## def __init__(self,num):
    2.                                                                                                                                                                                                                       
      # Define the function to be run by each thread. The function name must be run
    3. ## def run(self):
    4. ##    print("
    5. running on number:%s
    6. "%self.num)
    7. time.sleep(3)
    8. ## print("
    9. thread:%s
      "%self.num)
    10. ##if
    11. __name__ == '__main__':
      t1 = MyThread(1)
    12. ## t2 = MyThread(2)
    13. t1.start()
    14. t1.join()
    15. t2.start()
    16. ## t2.join()
    17. Output:
    18. running on number:1
    19. thread:1
    20. ##running on number:2
    21. ##thread:2
    22. The effect of setting parameters is as follows:

    1. if
      __name__ == '__main__':
      ## t1 = MyThread(1)
    2. t2 = MyThread(2)
    3. ## t1.start()
    4. ## t1.join(2)
    5. t2.start()
    6. ## t2.join()
    7. Output:
    8. ##running on number:1
    9. running on number:2
    10. ##thread:1
    11. ##thread:2
    12. Daemon
    13. By default, the main thread will wait for the end of all child threads when exiting. If you want the main thread not to wait for the child threads, but to automatically end all child threads when exiting, you need to set the child threads as background threads (daemon). The method is to call the setDaemon() method of the thread class.
    14. import time,threading

    1. ##def run(n):
    2. print("
      %s
    3. ".center(20,"
    4. *
      ") %n)
    5. time.sleep(2)## print("done
      ".center(20,"
    6. *
    7. "))
    8. ##def main():
    9. for
      i
      in
    10. range(5):
    11. ##   t = threading.Thread(target=run,args=(i,))
    12. t . start()
    13. t.join(1)
    14. ## print("
    15. starting thread
      ",t.getName())
    16. ##m = threading.Thread( target=main,args=())
    17. # Set the main thread to the Daemon thread, which serves as the daemon thread of the main thread of the program. When the main thread exits, The m thread will also exit, and other sub-threads started by m will exit at the same time, regardless of whether the execution is completed
    18. m.setDaemon(True)
    19. m.start()
    20. m.join(3)
    21. ##print("main thread done". center(20,"*"))
    22. Output:
    23. *********0**********
    24. starting thread Thread-2
    25. *********1*********
    26. ********done********
    27. starting thread Thread-3
    28. **********2************
    29. **main thread done **
    Thread lock (mutex lock Mutex)

    Multiple threads can be started under one process, and multiple threads share the memory space of the parent process. This means that each thread can access the same data. At this time, if two threads want to modify the same data at the same time, a thread lock is required.

    1. import time,threading
    2. ##def addNum():
    3. #Get this global variable in each thread
    4. global num
    5. print("
      --get num:",num)
    6. time.sleep(1)
    7. Perform -1 operation on this public variable
    8. num -= 1
    9. # Set a shared variable
    10. num = 100
    11. thread_list = []
    12. ##for
      i in range(100):
      ## t = threading.Thread(target=addNum)
    13. t.start()
    14. ## thread_list.append(t)
    15. # Wait for all threads to finish executing
    16. ##for
      t
    17. in
    18. thread_list:
      t.join()
    19. ##print("
    20. final num:
      ",num)
    21. Locked versionLock blocks other threads from sharing Resource access, and the same thread can only acquire once. If more than once, a deadlock will occur and the program cannot continue to execute.

    import time,threading

    1. ##def addNum():
    2. #Get this global variable in each thread
    3. global num
    4. print("
      --get num:",num)
    5. time.sleep(1)
    6. # Lock before modifying data
    7. ##lock.acquire()
    8. # Perform -1 operation on this public variable
    9. num -= 1
    10. # Release after modification
    11. ##lock.release()
    12. # Set a shared variable
    13. ##num = 100
    14. thread_list = []
    15. # Generate global lock
    16. lock
      = threading.Lock()
    17. ##for
    18. i
      in range (100):## t = threading.Thread(target=addNum)
    19. t.start()
    20. thread_list.append(t)
    21. # Wait for all Thread execution completed
    22. for
    23. t
    24. in
      thread_list: t.join()
    25. ##print("
      final num:
      ",num)
    26. GIL VS LockGIL guarantees that only one thread can execute at the same time. Lock is a user-level lock and has nothing to do with GIL.
    27. RLock (recursive lock)

      Rlock is allowed to be acquired multiple times in the same thread. The thread's release of shared resources requires the release of all locks. That is, n times of acquire require n times of release.

      def run1():

      1. ## print("grab the first part data")
      2. lock.acquire()
      3. global num
      4. ## num += 1
      5. ##lock
        .release()
      6. return num
      7. def run2():
      8. ## print("
      9. grab the second part data
      10. ")
      11. lock.acquire()
      12. ## ​ global num2
      13. num2 += 1
      14. lock
      15. .release()
      16. return
        num2
      17. ##def run3():
      18. ##​
        lock.acquire()
      19. res = run1()
      20. print ("
        between run1 and run2".center(50,"*"))
      21. ## res2 = run2 ()
      22. lock
        .release()
        ## print (res,res2)
      23. ##if
      24. __name__ == '__main__':
      25. num,num2 = 0,0
      26. num,num2 = threading.RLock()
      27. for i
        in
      28. range(10):
      29. t = threading.Thread(target=run3)
      30. # t.start()
      31. ##while
      32. threading.active_count() != 1:
      33. print(threading.active_count())
      34. #else
      35. :
      36. print("
        all threads done".center(50,"*"))
      37. Print(num,num2)The main difference between these two locks is that RLock allows to be acquired multiple times in the same thread. Lock does not allow this to happen. Note that if you use RLock, acquire and release must appear in pairs, that is, acquire is called n times, and release must be called n times to truly release the occupied lock.
      38. Semaphore (Semaphore)
      39. The mutex only allows one thread to change data at the same time, while Semaphore allows a certain number of threads to change data at the same time. For example, if the ticket office has 3 windows, then the maximum Only 3 people are allowed to buy tickets at the same time, and those at the back can only buy tickets after the people at any window in front leave.
      40. import threading,time

        1. ##def run(n):
        2. semaphore.acquire()
        3. ## time.sleep( 1)
        4. ## print("run the thread:%s"%n)
        5. semaphore.release()
        6. #if
        7. __name__ == '__main__':
        8. ##​ # Allows up to 5 threads to run at the same time
        9. semaphore = threading.BoundedSemaphore(5)
        10. ##for
        11. i
          in range(20):
        12. t = threading.Thread(target=run,args=(i,))
        13. t.start ()
        14. ##while threading.active_count() != 1:
        15. ​ # print(threading.active_count())
        16. ## pass
        17. else:
        18. ## print("
        19. all threads done
          ".center(50,"*"))
          Timer (timer)
        Timer calls a function at a certain interval , if you want to call a function every once in a while, you must set the Timer again in the function called by the Timer. Timer is a derived class of Thread.

          import threading
        1. ##def hello( ):
        2. print("
        3. hello,world!
        4. ")
          # Execute the hello function after delay 5 seconds
        5. t = threading.Timer(5,hello)
        6. t.start()
        7. Event
        8. Python provides the Event object for inter-thread communication, which has thread settings If the signal flag is false, the thread waits for the instruction signal to be set to true by other threads. The Event object implements a simple thread communication mechanism. It provides setting signals, clearing signals, waiting, etc. for realizing communication between threads.

        Set signal

        1. Use the set() method of Event to set the signal flag inside the Event object to true. The Event object provides the isSet() method to determine the state of its internal signal flag. When the set() method of the event object is used, the isSet() method returns true.

        Clear the signal

        1. Use the clear() method of Event to clear the signal flag inside the Event object, that is, set it to false. When using After Event's clear() method, the isSet() method returns false.

        Waiting

        1. Event's wait() method will only execute and complete the return quickly when the internal signal is true. When the internal signal flag of the Event object is false, the wait() method waits for it to be true before returning.

        2. Use Event to realize the interaction between two or more threads. Let's take the traffic light as an example. That is, start a thread to act as a traffic light, generate several threads to act as vehicles, and the vehicles will stop according to red and green. rule.

        import threading,time,random
        1. # #def light():
        2. if
        3. not
        4. event
          .isSet():
          # Every
        5. .
        6.       while True:
        7. ##                               ## count < 5:
        8. ##      print("\033[42;1m--green light on--\033[0m".center(50,"*"))
        9.         elif count < 8:
        10.             print("\033[43;1m--yellow light on--\033[0m".center(50,"*"))
        11.         elif count < 13:
        12.             ifevent.isSet():
        13.                 event.clear()
        14.             print("\033[41;1m--red light on--\033[0m".center(50,"*"))
        15.         else:
        16.             count = 0
        17.             event.set()
        18.         time.sleep(1)
        19.         count += 1
        20. def car(n):
        21.     while 1:
        22.         time.sleep(random.randrange(10))
        23.         ifevent.isSet():
        24.             print("car %s is running..."%n)
        25.         else:
        26.             print("car %s is waiting for the red light..."%n)
        27. if __name__ == "__main__":
        28.     event = threading.Event()
        29.     Light = threading.Thread(target=light,)
        30.     Light.start()
        31.     for i in range(3):
        32.         t = threading.Thread(target=car,args=(i,))
        33.         t.start()

        queue queue

        Queue in Python is the most commonly used form of exchanging data between threads. The Queue module is a module that provides queue operations.

        Create a queue object

        1. import queue
        2. q = queue.Queue(maxsize = 10)

        The queue.Queue class is a synchronous implementation of a queue. The queue length can be unlimited or limited. The queue length can be set through the optional parameter maxsize of the Queue constructor. If maxsize is less than 1, the queue length is unlimited.

        Put a value into the queue

        1. q.put("a")

        Call the put() method of the queue object to insert an item at the end of the queue. put() has two parameters. The first item is required and is the value of the inserted item; the second block is an optional parameter and defaults to 1. If the queue is currently empty and block is 1, the put() method causes the calling thread to pause until a data unit is freed. If block is 0, the put() method will throw a Full exception.

        Remove a value from the queue

        1. q.get()

        Call the get() method of the queue object to delete and return an item from the head of the queue. The optional parameter is block, which defaults to True. If the queue is empty and block is True, get() causes the calling thread to pause until an item becomes available. If the queue is empty and block is False, the queue will throw an Empty exception.

        Python Queue module has three queues and constructors

        1. # First in first out
        2. class queue.Queue(maxsize=0)
        3. First in, last out
        4. class queue.LifoQueue(maxsize=0)
        5. # The lower the priority queue level, the first it goes out
        6. ##class queue.PriorityQueue(maxsize=0)
        Common methods

        1. q = queue.Queue()
        2. # Returns the size of the queue
        3. q.qsize()
        4. # If the queue is empty, return True, otherwise False
        5. q.empty()
        6. If the queue is full, return True, otherwise False
        7. q.full()
        8. Get the queue, timeout waiting time
        9. q.get([block[,timeout]])
        10. # Equivalent to q.get(False)
        11. ##q.get_nowait()
        12. # Wait until the queue is empty before performing other operations
        13. q.join()

        Producer-Consumer Model

        Using the producer and consumer patterns in development and programming can solve most concurrency problems. This mode improves the overall data processing speed of the program by balancing the work capabilities of the production thread and the consumer thread.

        Why use the producer and consumer model

        In the thread world, the producer is the thread that produces data, and the consumer is the thread that consumes data. In multi-threaded development, if the producer's processing speed is very fast and the consumer's processing speed is very slow, then the producer must wait for the consumer to finish processing before continuing to produce data. In the same way, if the processing power of the consumer is greater than the producer, then the consumer must wait for the producer. In order to solve this problem, the producer and consumer models were introduced.

        What is the producer-consumer model

        The producer-consumer model uses a container to solve the strong coupling problem between producers and consumers. Producers and consumers do not communicate directly with each other, but communicate through blocking queues. Therefore, after the producer produces the data, it no longer waits for the consumer to process it, but directly throws it to the blocking queue. The consumer does not ask the producer for data, but Take it directly from the blocking queue. The blocking queue is equivalent to a buffer, balancing the processing capabilities of producers and consumers.

        The most basic example of the producer-consumer model.

        1. import queue,threading,time
        2. # #q = queue.Queue(maxsize=10)
        3. ##def Producer():
        4. count = 1
        5. ##while
          True:
        6.         q.put("
        7. bone%s
          "%count)
        8.                                                                                                               "
        9. produced bones
          ",count)
        10. Count += 1
        11. def Consumer(name):
        12. ##​
        13. while
        14. q.qsize () > 0:
          ##                  print("
          [%s] got [%s] and ate it... "%(name,q.
        15. get
          ()))## time.sleep(1)
        16. ##p = threading.Thread(target=Producer,)
        17. c1 = threading.Thread(target=Consumer,args=("王财",))
        18. ##c2 = threading.Thread (target=Consumer,args=("来福",))
        19. ##p.start()
        20. c1.start()
        21. ##c2.start()

The above is the detailed content of Python development--detailed explanation of processes, threads, and coroutines. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Django basic tutorial summaryNext article:Django basic tutorial summary

Related articles

See more