Day8 Socket Socketserver Exception Assertion

Source: Internet
Author: User

http://www.cnblogs.com/wupeiqi/articles/5040823.htmlSend

Http://www.cnblogs.com/alex3714/articles/5227251.html

Sendall constantly call send, one-time send past, transfer large files inappropriate

Socketserver is handled in a multi-threaded manner

Part of the Code

#_*_ Coding:utf8 _*_ImportSocketserverclassMytcphandler (Socketserver. Baserequesthandler):#Why is Baserequesthandler, see Alex Blog, it's under the other things    #there must be a handle method    defhandle (self):#all interactions with the client are here.        #Pass        Print("New Conn:", self.client_address)#Print the address of the clientdata = SELF.REQUEST.RECV (1024)        Print("Client Says:", Data.decode ()) self.request.send (data)if __name__=="__main__": HOST, PORT="localhost", 50007#Send the class that you just wrote as a parameter to the Threadingtcpserver class, and the following code creates a caseServer =Socketserver. Threadingtcpserver (HOST, PORT), Mytcphandler)#starting this server, the server will run until CTRL + C stopsServer.serve_forever ()

Add while true to achieve the effect that multiple clients can connect, multiple concurrent. The client or the previous piece of code is almost unchanged.

Exception handling
Try
Pass
Except

Finally
Whether or not an error is performed
Else
Do it without a fight.
Start your own abnormal raise

Exception Universal Anomaly

Custom Exceptions

Some business logic does not meet the need to define your own exceptions

Http://www.cnblogs.com/wupeiqi/articles/5017742.html

classwupeiqiexception (Exception):def __init__(Self, msg): Self.message=msgdef __str__(self):returnSelf.messageTry:    RaiseWupeiqiexception ('My Exception')exceptwupeiqiexception,e:PrintE

Assertion

Assert that the condition is not valid, error

The function shows clearly that the condition must conform to

Join waits for thread to finish executing

The difference between threads and processes
GIL Global Interpreter Lock
Prevents native threads from modifying a single piece of data at the same time, is non-thread-safe (cannot guarantee data consistency), requires lock
PY call C's native thread, unable to control the original voice of C, start 10 native threads, may be the data that disorderly, Py can't control that just let a time one thread modify
This data, really runs only one native thread

Threads can share data, and processes cannot, by default, need to pass through third-party pipelines or queues (q), pipe

The thread lock and the Gil do not have any relationship, the difference:
Gil restricts only one thread to run, and the interpreter switches threads back and forth so that each thread gets run,
When the first thread runs out of results, it is not assigned to a global variable, the second one runs, and the second runs out and assigns the result to the global variable.
The interpreter switches to the first thread, and the first thread will pay back the result of the assignment, and it will flush out the value of the second thread.
The Gil controls this piece of the interpreter, preventing the data from being removed from the layer below the interpreter. The thread lock is to prevent the data on the interpreter from being arbitrarily modified.

No communication between cities
The process is more than the machine hangs, so the process pool needs to be set up (pool)
Callback callback can pass the result of the process to the parent process

Day8 Socket Socketserver Exception Assertion

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.