day40, epoll, database concepts

                                                                                   epoll, database concepts

Prior to speaking, the multiplex select, it has a disadvantage that the data is too large, but to process SELECT (beyond 1024 clients)

select only handle 1024 simultaneous clients,

Multithreading experience resource bottlenecks, what is the most effective way to solve it high concurrency

linux provides multiplexed IO model epoll this, pay attention to other platforms without a corresponding implementation

So in epoll is available only in linux


select processing multiple client connection mode service side:

  1, after each finished with a read and write, we need to repeat the punch used, including the removal process, adding process, the default process will be added to the waiting queue, and blocking the process of living, but the waiting queue and update operations infrequently.

  2, the SELECT can not learn in the process of which socket is the data you need to traverse

epol l problem to be solved

    1. avoid frequent operation of the queue

    2. Avoid traverse all socket

 

  So the first question epoll, adopted a program, it will wait queue and maintenance, the blocking process to split the two operations

   In epoll register and unregister the function for maintaining a waiting queue

  register process is added to the waiting queue unregister the process is removed from the waiting queue, such an action would avoid the need to re-treatment every operational problems waiting queue

Using these two functions to control our own wait queue to add and remove in order to avoid frequent operation queue

, The relevant code is as follows

Import socket, the SELECT 
Server = socket.socket () 
server.bind (( " 127.0.0.1 " , 1688 )) 
server.listen ( 5 ) 

# create epoll event object, the follow-up to monitor the event to which the 
epoll = select.epoll ()
 # registration server listens fd to wait for read event collection 
epoll.register (server.fileno (), select.EPOLLIN)    # need to focus on this server socket readable event 

# wait for events 
the while True:
     for our sock, event in epoll. poll ():
     Pass
The second problem is in the process can not select which socket is to know which data needs to traverse so

epol To solve this problem, maintain a ready list in the kernel,

1. Create an object epoll, epoll also corresponds to a file, the file system management

2. When execution register, add objects to epoll wait queue of socket

3. When data arrives, CPU executes an interrupt program, copy the data to a socket

4. In epoll, the interrupt program will be executed next epoll object callback function, passing Ready socket object

5. socket, ready to add to the list

6. Wake epoll queue waiting process,

After the wake-up process, due to the ready list, so no need to traverse the socket, you can deal directly with the ready list

To solve these two problems, the amount of concurrency has been greatly improved, while maintaining the maximum level of thousands of socket

epoll correlation function

Import select Import module select 

epoll = select.epoll () objects to create a epoll 

file handle and events epoll.register (file handle, event type) registered to monitor 

the type of event: 

  select.EPOLLIN readable event 

  select.EPOLLOUT can write event 

  select .EPOLLERR error event 

  select.EPOLLHUP client disconnection events 

epoll.unregister (file handle) destroyed the file handle 

epoll.poll (timeout) when a file handle changes will be in the form of a list of active reporting to the user process, timeout 

                     to timeout default is -1 , i.e., waits until the file handle changed, if designated as a 

                     so epoll report every 1 second change of the current file handle return null if no change 

epoll.fileno () returns the control file is described epoll character (the Return the epoll the Control File descriptor) 

epoll.modfiy (fineno, event) fineno file descriptors for the event type event descriptor is to modify the action event corresponding 

one from the specified file descriptor epoll.fromfd (fileno) Creating an epoll objects

epoll.close () to close the control object descriptor epoll
View Code

Client:

# Coding: UTF. 8- 
# client 
# Create client socket objects 
Import socket 
ClientSocket = socket.socket (socket.AF_INET, socket.SOCK_STREAM)
 # server IP address and port number tuple 
the server_address = ( ' 127.0.0.1 ' , 1688 )
 # client connections specified IP address and port number 
clientsocket.connect (the server_address) 

the while True:
     # of input data 
    data = the raw_input ( ' Please iNPUT: ' )
     IF data == " Q " :
         BREAK 
    IF  Not data:
      Continue 
    # client sends data 
    clientsocket.send (data.encode ( " UTF-. 8 " ))
     # client receives data 
    SERVER_DATA = clientsocket.recv (1024 )
     Print ( ' data received by the client: ' , SERVER_DATA)
 # Close The client socket 
clientsocket.close ()
View Code

Server:

# coding:utf-8
import socket, select

server = socket.socket()
server.bind(("127.0.0.1", 1688))
server.listen(5)

msgs = []


fd_socket = {server.fileno(): server}
epoll = select.epoll()
# 注册服务器的 写就绪
epoll.register(server.fileno(), select.EPOLLIN)

while True:
    for fd, event in epoll.poll():
        sock = fd_socket[fd]
        print(fd, event)
        # Returns a file descriptor needs to obtain the corresponding socket 
        IF our sock == Server:   # If the server accepts the request 
            Client, addr = server.accept ()
             # registered client write-ready 
            epoll.register (client.fileno (), select .EPOLLIN)
             # Add the correspondence between 
            fd_socket [client.fileno ()] = Client 

        # read ready 
        elif event == select.EPOLLIN: 
            the Data = sock.recv (2018 )
             IF  not the Data:
                 # logout event 
                epoll.unregister (fd)
                 # Close socket
                sock.close ()
                 # delete the correspondence between the socket 
                del fd_socket [fd]
                 Print ( " Somebody Fuck OUT ... " )
                 the Continue 

            Print (data.decode ( " UTF-8 " ))
             # read the data needed to send data back so Next, change the write-ready = event 
            epoll.modify (fd, select.EPOLLOUT)
             # recording data 
            msgs.append ((our sock, data.upper ()))
         elif event == select.EPOLLOUT:
             for Item in Msgs [:] :
                 IF Item [0] ==our sock: 
                    sock.send (Item [ 1 ]) 
                    msgs.remove (Item) 
            # switching events of interest to the write-ready 
            epoll.modify (fd, select.EPOLLIN)
View Code

Some basic principles of database:

 

What is the database:

  TCP is essentially a database program CS structure,

  The client connects to the server transmits to the server instruction, the operation to complete the data

 

Correspondence between the database and file system:

  A data item name = jerry       is essentially part of a row in the data file

  A record jerry, 18, man      is essentially a row of data files

  A table                                    is essentially a file

  Database Folder                                   

  Server-side program DBMS DataBaseManagerSystem database management system database

  The database server                         running the DBMS computer

Installation: master

  1. Download extracting package

  2. Unzip to a directory

  3. Add the environment variable

       The system will copy the bin to add the full path to where the path of

  4. It should be self-starting as a server mysql server Requires System Services

      mysqld --install input services run to see whether success

     To delete a service sc delete mysql If you need to reinstall, then ...

     Start Service net start mysql

     Stop service net stop mysql

Server connection instruction

     TCP is the essence of the program, you must specify the ip and port, if the server is running can be omitted if the port did not turn over ip port can also be omitted in the machine

    Complete wording:

     

mysql -hip -P port -u username - the p-code   
examples: MySQL -uroot-- the p- 
    
MySQL 5.6 default is no password

Modify the administrator password:

   1. If you know the original password can use this tool mysqladmin

mysqladmin -p old password - U username password new password 
instance: mysqladmin -uroot--p password 123

  2. do not know the circumstances of the original password:

     Delete the password file, deletes all licensing information

     Skip authorization table we can specify allowed to ignore the authorization information when the server is started

    1. Turn off the server performs mysql mysqld --skip-grant-tables directly to the terminal

    2. No root account password

    3. Perform the update statement

         update mysql.user set password = password("123") where user="root" and host = "localhost";

 

Guess you like

Origin www.cnblogs.com/WBaiC1/p/11006129.html