Day24 stage summary

Posted by the_lynx123 on Mon, 31 Jan 2022 21:21:48 +0100

day24 stage summary

Curriculum objective: summarize and test the knowledge points in the third module stage, so that students can better master the relevant knowledge of this module.

Course overview:

  • Knowledge supplement
  • Stage summary (mind map)
  • Examination questions

1. Supplement of knowledge points

1.1 concurrent programming & Network Programming

From the perspective of knowledge points, there is no relationship between the two:

  • Network programming, based on the basic knowledge of network and socket module, realizes the data transmission of network.

  • Concurrent programming is based on multi process and multi thread to improve the execution efficiency of the program.

However, in many "frameworks", they will actually combine the two, and use multi process, multi thread and other means to improve the processing efficiency of network programming.

Case 1: multithreaded socket server

The socket server is implemented based on multithreading to process the requests of multiple clients at the same time.

  • Server

    import socket
    import threading
    
    
    def task(conn):
        while True:
            client_data = conn.recv(1024)
            data = client_data.decode('utf-8')
            print("Messages received from clients:", data)
            if data.upper() == "Q":
                break
            conn.sendall("Yes, yes".encode('utf-8'))
        conn.close()
    
    
    def run():
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('127.0.0.1', 8001))
        sock.listen(5)
        while True:
            # Waiting for client to connect (main thread)
            conn, addr = sock.accept()
            # Create child thread
            t = threading.Thread(target=task, args=(conn,))
            t.start()
            
        sock.close()
    
    
    if __name__ == '__main__':
        run()
    
    
  • client

    import socket
    
    # 1. Send a connection request to the specified IP
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(('127.0.0.1', 8001))
    
    while True:
        txt = input(">>>")
        client.sendall(txt.encode('utf-8'))
        if txt.upper() == 'Q':
            break
        reply = client.recv(1024)
        print(reply.decode("utf-8"))
    
    # Close the connection. When closing the connection, empty data will be sent to the server.
    client.close()
    

Case 2: multi process socket server

socket server is realized based on multi process, which can process the requests of multiple clients at the same time.

  • Server

    import socket
    import multiprocessing
    
    
    def task(conn):
        while True:
            client_data = conn.recv(1024)
            data = client_data.decode('utf-8')
            print("Received message from client:", data)
            if data.upper() == "Q":
                break
            conn.sendall("Yes, yes".encode('utf-8'))
        conn.close()
    
    
    def run():
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.bind(('127.0.0.1', 8001))
        sock.listen(5)
        while True:
            # Waiting for client to connect
            conn, addr = sock.accept()
            
            # Child process created (at least threads)
            t = multiprocessing.Process(target=task, args=(conn,))
            t.start()
            
        sock.close()
    
    
    if __name__ == '__main__':
        run()
    
  • client

    import socket
    
    # 1. Send a connection request to the specified IP
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect(('127.0.0.1', 8001))
    
    while True:
        txt = input(">>>")
        client.sendall(txt.encode('utf-8'))
        if txt.upper() == 'Q':
            break
        reply = client.recv(1024)
        print(reply.decode("utf-8"))
    
    # Close the connection. When closing the connection, empty data will be sent to the server.
    client.close()
    

1.2 concurrency and parallelism

How to understand these concepts?

  • Serial, multiple tasks are queued and executed one by one in order.

  • Concurrency: assuming that there are multiple tasks with only one CPU, only one task can be processed at the same time. In order to avoid serialization, the tasks can be switched to run (each task runs a little, and then switched) to achieve the effect of concurrency (it seems that they all run at the same time).

    Concurrent in Python Embodied in the code: collaborative process and multithreading (by CPython of GIL Lock limit, multiple threads cannot be locked CPU Scheduling).
    
  • In parallel, if there are multiple tasks and multiple CPUs, then each CPU executes one task at the same time, and the tasks can really run at the same time.

    Parallel in Python Embodiment in the code: multi process.
    

1.3 single instance mode

In python development and source code, there are two most common writing methods for singleton mode, namely:

  • Based on__ new__ Method implementation

    import threading
    import time
    
    class Singleton:
        instance = None
        lock = threading.RLock()
    
        def __init__(self):
            self.name = "Wu Peiqi"
            
        def __new__(cls, *args, **kwargs):
    
            if cls.instance:
                return cls.instance
            with cls.lock:
                if cls.instance:
                    return cls.instance
                # time.sleep(0.1)
                cls.instance = object.__new__(cls)
            return cls.instance
        
        
    obj1 = Singleton()
    obj2 = Singleton()
    
    print(obj1 is obj2) # True
    
  • Module based import method

    # utils.py
    
    class Singleton:
        
        def __init__(self):
            self.name = "Wu Peiqi"
            
        ...
            
    single = Singleton()
    
    from xx import single
    
    print(single)
    
    from xx import single
    print(single)
    

2. Stage summary

Topics: Python