Regarding the code of using the pythongevent frame as the TCP server, is there any method for receiving messages from each socket using event listening callback?

Source: Internet
Author: User
Regarding the pythongeventTCP server, when each client connects to the TCPServer, gevent will automatically allocate a greenlet for execution. But how can I monitor the message reception of each sokcet client, the code we see is to use the circular receipt in the greenlet method. Is there no callback? Regarding the python gevent TCP server, when each client connects to the TCPServer, gevent will automatically allocate a greenlet for execution. But how can I monitor the message reception of each sokcet client, the code we see is to use the circular receipt in the greenlet method. Is there no callback?

Reply: gevent has a major advantage over other frameworks (such as tornado and twisted) in that it uses synchronous methods (naturally no callback function) to write asynchronous applications, because the synchronous method is closer to the developer's programming thinking.
Gevent can be explained to pythoner in one sentence: the event listening to file descriptors is implemented using multiple IO multiplexing, which can stimulate the "transparent" switching of the coroutine. This sentence is easy to say, but it is more complicated to elaborate:
  1. The underlying layer (or master coroutine) naturally has a multiplexing loop (epoll in linux and kqueue in unix). epoll is used in place of the description below)
  2. When processing a socket link, a coroutine greenlet is created for processing.
  3. When the socket is blocked, such as waiting for data to be returned or sent, gevent performs two key steps:
    1. Add a readable or writable Event Callback for the socket fd on epoll, and this callback function is gevent. getcurrent (). switch
    2. Use get_hub (). switch () to switch to the master coroutine. Switch back to the main coroutine to do other things. However, when the socket is readable or writable, epoll will naturally call the added callback function to switch back to the processing coroutine of the socket and then execute it from the last Hanging Point.
The reason for transparency is that patch is installed on python socket. The so-called patch is to implement a socket module to replace the standard socket module of python.
Def patch_socket ():
From gevent import socket
_ Socket = _ import _ ('socket ')
_ Socket. socket = socket. socket
...
Compared with the standard socket module of python, the socket module implemented by gevent has the following modifications:
  1. Set all sockets to non-blocking.
  2. Modify the key functions, such as send and recv, and add a callback function in the socket fd when an exception EWOULDBLOCK is caught.

To sum up, gevent is not a callback without event listening, but a patch for python socket to make it transparent, so that programmers can use the synchronous thinking for development.
Ps, patch for python socket, although good, but there is a small drawback, is: The socket in the python c extension module is not controlled and scheduled by gevent. Therefore, the network library used in gevent should use the pure python library whenever possible.. Here, this loop is the cycle that drives the entire session of a client. The whole echo is executed in a greenlet when a customer connects to the StreamServer listening port as a callback.

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.