"Python automated operation and maintenance road DAY10" Network Socketserver

Source: Internet
Author: User
Tags epoll

Class notes:

Last week review:
Socket
1. Import the module
2. Create a socket
3. Send
Sendall, Sendall is essentially a call to send
But inside a while loop, get the message, loop the hair, know the message is completely sent
Sendall:
While True:
Ten = Send (' ADKFLSKJF023JLSKDF ')
Ten = Send (' ADKFLSKJF023JLSKDF ')
...
Send, which may not be completely sent at once
Ten = Send (' ADKFLSKJF023JLSKDF ')
So, for later use, you should use Sendall

A byte is sent in Python 3, and a string is sent in the Python2

RECV (2048) accepts up to 2048
Sticky Bag:
ACK Acknowledgement

Socketserver:
1. Custom Classes
2. Inheritance Methods

I. Previous work:


Two. Small Knowledge points:
Scope
1. There is no block-level scope in Python
Java/c#, you can't.
Python/javascript can

If 1 = = 1:
name = ' Alex '
Print (name)

For I in range (10):
name = I
Print (name)

2. Using functions as scopes in Python
3. Scope chain, look for the time, from the inside out, know that no error can be found

For action, the scope has been fully determined before the function has been executed, and the scope chain has been determined.


Three. Python2.7 multiple inheritance, review the Python3.5 of multiple inheritance
All classes in Python3 inherit the object class by default
All classes in Python2.7 do not inherit the object class by default

In Python2.7:
Classes that do not inherit object are not supported by default, called Classic classes;
The class that inherits object, which is called the new class, adds object;

In Python3:
All of them are new, and they inherit the object class by default.

Four. Socketserver source, down to find the source
============== ***** =================
Supports concurrent processing of sockets

1. __init__ TCPServer, calling Baseserver constructor method
2. Baseserver
Self.server_address = server_address incoming tuple, IP and port
Self. Requesthandlerclass = Requesthandlerclass A class of its own definition
Self.__is_shut_down = Threading. Event ()
Self.__shutdown_request = False
3. Server.serve_forever ()--"baseserver
4. Called in Self._handle_request_noblock () Serve_forever ()!
5. Self._handle_request_noblock Method Invocation: Self.process_request (Request, client_address)
6. Call from Self.process_request (Request, client_address) method:
Self.finish_request (Request, client_address)
Self.shutdown_request (Request)
7. Self.finish_request (Request, client_address)
Self. Requesthandlerclass (Request, client_address, self)
8. Shutdown_request (self, request):
Self.close_request (Request)

Five. I/O multiplexing
Ability to detect all IO operations, not just sockets, except for file operations
Overview:
Select, poll, Epoll
Does the inside of the socket object change?
When does it change?
When you connect or send and receive messages
A For loop is maintained at the bottom of the select System core to detect changes. Both Windows and Linux platforms are available to support cross-platform. There are c10k problems, maximum support of 1024
Poll, the underlying is also implemented through a for loop. But there is no limit to the number. But the for loop is inefficient.
Epoll, who changes (callback function), tells Epoll who has changed. and


Six. Multithreading, multi-process
Overview:

Alex Zhen Huan West Tour
1. An application can have multi-process, multi-threaded
2. Default is single process, single thread
3. Single-process, multi-threading, does not improve performance in Python and can be promoted in Java and C #.
Increase Concurrency:
Multithreading: IO operation, not CPU, efficiency improvement is possible

Multi-process: computational operation, CPU-intensive, so performance is not improved
You can use multiple processes
4. GIL, the existence of a global interpreter lock, resulting in

To create a thread knowledge point:
Obj.setdaemon (False) #True, which indicates that the main thread is unequal to child threads
Obj.join (num) #表示主线程到此等待 ... Until the end of the child thread execution; Num is the maximum number of seconds to wait for Num
Obj.start () #不代表当前县城内会被立即执行

Blog, to be continued ...

"Python automated operation and maintenance road DAY10" Network Socketserver

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.