Python's process and IO operations

Source: Internet
Author: User
Tags epoll goto network function readable switches

Co-process

Co-process, also known as micro-threading, fiber. English name Coroutine.

The concept of the co-process was raised early, but it has not been widely used in some languages (such as LUA) in recent years.

subroutines, or functions, are hierarchical calls in all languages, such as a call to b,b in the execution of a call to C,c execution complete return, b execution is finished, and finally a executes.

So the subroutine call is implemented through the stack, and a thread is executing a subroutine.

Subroutine calls are always one entry, one return, and the call order is clear. And the call of the association and subroutine is different.

The process appears to be a subroutine, but during execution, it can be interrupted inside the subroutine, then execute another subroutine, and then return to execute at the appropriate time.

The operation of the thread and process is triggered by the program to trigger the system interface, the final performer is the system, and the operation of the coprocessor is the programmer.

The process can be considered a user-space thread, with 2 main advantages over traditional preemptive threads:

    • Unlike threads, the co-process is itself voluntarily yielding the CPU and delivering the next run that he expects, rather than being interrupted by a system schedule at any time. Therefore, the use of the process is more clear and understandable, and in most cases does not require a locking mechanism.
    • Compared to threads, the handoff of a process is controlled by program control, which occurs in user space rather than kernel space, so the cost of switching is very small.
    • In a sense, the relationship between the threads and the thread is similar to the thread-to-process relationship, and multiple threads run in the context of the same thread.

The significance of the existence of the process: for multi-threaded applications, the CPU by slicing the way to switch between threads of execution, thread switching takes time (save state, next continue). , only one thread is used, and a code block execution order is specified in one thread.

Application scenario: When there are a large number of operations in the program that do not require the CPU (IO), it is suitable for the association process;

Benefits of the co-process:

    • No overhead for thread context switching
    • No need for atomic operation locking and synchronization overhead
      • "Atomic operations (atomic operation) do not require synchronized", so-called atomic operations are operations that are not interrupted by the thread scheduling mechanism; Once this operation starts, it runs until the end, and there is no context switch in the middle. (Switch to another thread). An atomic operation can be a step or multiple steps, but its order cannot be disrupted, or it can be cut off only the execution part. As a whole is the nucleus of atomicity.
    • Easy switching of control flow and simplified programming model
    • High concurrency + high scalability + Low cost: A CPU support for tens of thousands of processes is not a problem. Therefore, it is suitable for high concurrency processing.

Disadvantages:

    • Unable to take advantage of multicore resources: The nature of the process is a single thread, it can not be a single CPU at the same time multiple cores, the process needs and processes to run on multi-CPU. Of course, most of the applications that we write in the day-out are not necessary, except for CPU-intensive applications.
    • Blocking (Blocking) operations (such as IO) can block the entire program

Differences between processes, threads, and co-routines

Process:

Processes do not share any state, the scheduling of the process by the operating system, each process has its own independent memory space, inter-process communication is mainly through the way of signal transmission to achieve, the implementation of a variety of ways, signal volume, pipelines, events, any one way of communication efficiency is required over the core, resulting in low communication efficiency. Because it is a separate memory space, the context switch needs to save the information of the first call stack, the information of the CPU registers, the virtual memory, and the related handle of the open, so the switching between the context process is expensive and the communication is troublesome.


Thread:

Shared variables between threads, solve the problem of communication problems, but for variable access needs to lock, thread scheduling is mainly the operating system, a process can have more than one thread, but each thread will share the parent process like the operating system request resources, this includes virtual memory, files, etc., because it is a shared resource, So the system resources required to create a thread are much smaller than the process, and the corresponding number of threads that can be created becomes a lot more. Thread-time communication can also communicate through shared memory, in addition to the way communication between processes can be used, so this is much faster than passing through the kernel. In addition, in terms of scheduling is also due to the memory is shared, so the context switches need to save things like the less, so that the context of switching also become efficient.


Co-process:

The scheduling of the process is entirely user-controlled, a thread can have multiple threads, the user creates a few, and then each thread loops through the specified list of tasks, performs the next task when the task is blocked, and returns to perform the task when it is resumed. The switch between tasks only need to save the context of each task, just like the direct Operation Stack, so that there is no kernel switching overhead, can not lock the access to global variables, so the context of the switch is very fast, in addition to the process also need to ensure non-clogging and no interdependence, the process is basically unable to synchronize communication, More use of one-step message communication, high efficiency.

Network programming model

Let's start with a brief review of some commonly used network programming models. The network programming model can be broadly divided into two types: synchronous model and asynchronous model.

    • Synchronization model:

The synchronization model uses the blocking IO mode, which blocks the thread until the IO completes or fails when the IO function is blocked by blocking IO mode with read. A typical representation of a synchronization model is the Thread_per_connection model, which creates a new thread to service the read/write of the new socket whenever the accept call on the main thread returns. The advantage of this model is that the program logic is concise and conforms to the human mind; The disadvantage is that the scalability is limited by the number of threads, and as more and more threads are connected, frequent thread switching can seriously slow down performance and have to deal with multi-threaded synchronization issues.

    • Async Model:

Asynchronous models generally use non-blocking IO mode and are combined with epoll/select/poll multiplexing mechanisms. The non-blocking mode is lowered with read, and returns immediately if no data is readable and notifies the user that there is no readable (eagain/ewouldblock), rather than blocking the current thread. The asynchronous model enables a single thread to serve multiple IO objects at the same time. The typical representation of an asynchronous model is the reactor model. In the reactor model, we register all IO events to be processed in a central IO multiplexer (typically Epoll/select/poll), while the main thread blocks on the multiplexer. Once an IO event arrives or is ready, the multiplexer returns and distributes the corresponding IO event to the corresponding processor (that is, the callback function), and finally the processor calls the Read/write function for IO operations.

The asynchronous model is characterized by much better performance and scalability than the synchronous model, but its structure is complex and not easy to write and maintain. In the asynchronous model, the code before IO (the submitter of the IO Task) and the processing code after IO (the callback function) are fragmented.

Co-process and network programming

The emergence of the process is a drawback to overcoming the synchronization model and the asynchronous model, and it offers the possibility of combining their advantages: Now suppose we have 3 a,b,c to do several IO operations, respectively. These 3 processes run in the same scheduler or thread context and use the CPU in turn. The scheduler maintains a multiplexer (Epoll/select/poll) within it. The process a runs first, when it executes to an IO operation, but the IO operation is not immediately ready, a registers the IO event with the scheduler, and abandons the CPU voluntarily. The scheduler then switches B to start execution on the CPU, and similarly, when it encounters an IO operation, registers the IO event with the scheduler and actively discards the CPU. The scheduler switches C to start execution on the CPU. When all the threads are "blocked", the scheduler checks whether the registered IO events are occurring or ready. Assuming that the IO time that is registered at this time is already in place, the scheduler will resume B execution and B will then run down from where the CPU was last discarded. A and C are the same. Thus, for each of the threads, it is a synchronous model, but for the entire application it is an asynchronous model.

Programming paradigm

The programming paradigm (programming Paradigm) is a typical programming style of a programming language or a programming approach. With the development of programming methodology and software engineering, especially the popularization of OO ideas, the terms of paradigm (PARADIGM) and programming paradigm are appearing in front of people. Object-oriented Programming (OOP) is often hailed as a revolutionary idea, because it differs from other programming paradigms. The programming paradigm is perhaps the most important term to understand when learning any programming language.

After Thomas Kuhn put forward the paradigm of "scientific Revolution", Robert Floyd used the term "programming paradigm" in the 1979 Turing Award presentation. The programming paradigm typically consists of three aspects, with OOP as an example:

    1. The logical system of discipline--rule paradigm: such as Class/object, inheritance, dynamic binding, method rewriting, object substitution and so on.
    2. Psychological cognition factor--psychological paradigm: According to the view of the father Alan Kay of object-oriented programming, "calculation is simulation". OO paradigm attaches great importance to the value of metaphor (metaphor) and simulates nature in a natural way by personification.
    3. Natural View/World Outlook-concept paradigm: Emphasizing the organization of the program, depending on the program as a loosely-coupled set of objects/classes, the inheritance mechanism organizes the classes into a hierarchical structure, and the program runs as a dialogue between the objects that serve each other.

Simply put, the programming paradigm is the view that programmers should have in view of the program.

To further deepen our understanding of the programming paradigm, here are some of the most common programming paradigms.

Again, it is important to note that the programming paradigm is a classification of programming languages and is not intended for a programming language. In the case of programming languages, a programming language can also be used in a variety of programming paradigms.

Procedural (imperative) programming

Procedural programming, also known as imperative programming, should be the most primitive and a traditional way of programming that we are most familiar with. In essence, it is the abstraction of the operating mechanism of "Von Neumann Machine", which is based on the sequential arrangement of computer instructions.

(That is, the procedural language simulates the system structure of a computer machine, not the individual abilities and tendencies of a language-based user.) We should be very clear on this point, such as: We have used the first MCU assembly language. )

The steps for procedural programming are:

First, we must abstract the solution of the problem to be solved as a series of conceptualized steps. These steps are then programmatically translated into program instruction sets (algorithms), which are arranged in a certain order to illustrate how to perform a task or solve a problem. This means that the programmer has to know what the program is going to do and tell the computer how to do the required calculations, including every detail operation. In short, it is to treat a computer as a device that finish obey commands.

Therefore, in the process of programming, it is a key step to standardize and abstract the problem to solve the problem. Secondly, it is the correct solution to write the specific algorithm and complete the corresponding algorithm implementation problem. Of course, the ability of programmers to deal with the abstraction of a problem is also a very important factor, but it has nothing to do with programming languages.

Program Flowchart is an effective assistant method for programming of process language.

Although there are many existing computer programming languages, all programming languages that support the process programming paradigm are summed up in a procedural programming language. For example, machine language, assembly language, BASIC, COBOL, C, FORTRAN, language, and many other third-generation programming languages are summarized into the process of language.

The procedural language is particularly suited to solving linear (or step-by-step) algorithmic problems. It emphasizes the design approach of "top-down" and "excellence". This approach is very similar to our work and lifestyle, because our daily activities are carried out in a sequential order.

Procedural languages tend to develop programs that run faster and have higher utilization of system resources. The procedural language is very flexible and powerful, and there are a number of classic application examples that allow programmers to use it to solve a variety of problems.

The disadvantage of a procedural language is that it is not suitable for solving certain kinds of problems, such as unstructured problems with complex algorithms. The problem is that the procedural language must explain an algorithm in detail and include the order in which the instructions or statements are executed. In fact, it is extremely difficult to give detailed algorithms to unstructured problems with complex algorithms.

A widely contentious and controversial place is the unconditional branch, or Goto statement, which is part of most procedural programming languages, and opponents claim that a goto statement can be misused indefinitely; it gives the program a chance to create confusion. The current consensus is that it should be kept in most languages, and that it should be minimized in terms of the risks it has.

Event-driven programming

In fact, event-driven programming has been applied to program design long before the graphical user interface (GUI) appears, but only when the graphical user interface is widely popular, it gradually evolved into a widely used programming mode.

In procedural design, the code itself gives the order in which the program executes, although the order of execution may be affected by the program's input data.

In an event-driven program design, many parts of the program may be executed at a completely unpredictable time. Often, the execution of these programs is motivated by the interaction of the user with the program being executed.

    • event . is to inform that a particular event has occurred (the occurrence is random).
    • events and polling . The conduct of polling is constantly observed and judged, and is an endless form of behavior. And the event is quietly waiting for things to happen. In fact, before Windows appears, PC applications that use the mouse input character mode must perform serial polling and in this way query and respond to different user exercises.
    • event handler . is a piece of program code that executes when a response is made to an event. The event handler enables the program to respond to the user's behavior.

Event drivers are often used for user interaction with the program, interacting interactively through a graphical user interface (mouse, keyboard, touchpad). Of course, it can also be used for exception handling and responding to user-defined events, and so on.

Event exception handling is more complex than user interaction.

Event-driven is not confined to GUI programming applications only. But we also need to consider more practical issues such as event definition, event triggering, event transformation, event merging, event queuing, event dispatch, event handling, events, and so on.

In fact, so far, we have not found the language for pure event-driven programming and a similar development environment. All event-driven data is based on GUI events.

The event-driven programming languages are: VB, C #, Java (GUI of Java swing), etc. Most of the events they involve are GUI events.

Object-Oriented Programming

A procedural paradigm requires programmers to look at each problem with a step-by-step algorithm. It is clear that not every problem fits into this process of thinking. This has led to the emergence of other program design paradigms, including the object-oriented programming paradigm we are introducing now.

The object-oriented programming pattern has been in the more than 20 years, after the development of these years, its design ideas and design patterns have steadily entered the mainstream of programming language. The top three Java, C, C + +, Java and C + + are all object-oriented programming languages from the Tiobe programming Community2010 Year November programming language rankings.

Object-oriented programming includes three basic concepts: encapsulation, inheritance, polymorphism. Object-oriented programming language supports object-oriented design paradigm through classes, methods, objects, and message passing.

1. Objects

Everything is the object of the world.

The abstract mechanism of object-oriented program design is to abstract the problem to be solved as an object in object-oriented program. Use encapsulation to make each object have an individual identity. The program is a pile of objects, passing through the message, requesting other objects to work.

2. Class

Each object is an entity in its class.

Birds of a feather--that is, the class is a collection of similar objects. The object in the class can accept the same message. In other words: The class contains and describes a set of objects that have common attributes (data elements) and common behaviors (functions).

For example: apples, pears, oranges and other objects belong to the fruit category.

3. Encapsulation

Encapsulation (sometimes referred to as information hiding) is the process of combining data and behavior in a package and hiding the data from the user of the object. Information hiding is the basic principle of object-oriented programming, and encapsulation is a way to realize this principle.

Encapsulation enables objects to present a "black box" feature, which is a key step in the reuse and reliability of objects.

4. Interface

Each object has an interface. An interface is not a class, but a set of specifications for a class that conforms to the requirements of the interface. The interface describes what the class should do but does not specify how it should be done. A class can have one or more interfaces.

5. Methods

method determines what kind of message an object can accept. Object-oriented design is sometimes simply summed up as "sending messages to objects".

6. Inheritance

The idea of inheritance is to allow new classes to be built on the basis of existing classes. A subclass can inherit all members of the parent class, including properties and methods.

The primary role of inheritance is to accomplish code reuse by implementing inheritance, and the completion of code reuse through interface inheritance. Inheritance is a technique of specification, not a technique of implementation.

7. polymorphic

Polymorphism provides "interface and implementation separation". Polymorphism can not only improve the structure and readability of the program, but also develop the "expandable" program.

Inheritance is the basis of polymorphism. Polymorphism is the purpose of inheritance.

It can enhance the simplicity, flexibility, maintainability, reusability and Expansibility of the program by using polymorphism based on class inheritance, multi-state based on interface inheritance, and polymorphism based on template.

The object-oriented technology, on the one hand, draws on the thinking way of philosophy, psychology and biology, on the other hand, it is based on other programming techniques and is the natural product of previous programming thought.

If the structural software design is to apply the functional programming technology to the command language programming, object-oriented programming is just another way to apply the functional model to the imperative program, at this point, the module progresses to the object, the process is holed up to the class member method. Many of the technologies of OOP--abstract data types, information hiding, interface and implementation separation, object generation, messaging, and so on--are many things that are owned by structured software design or appear separately in other programming languages. But only in the object-oriented language, they appear together, in a unique way of cooperation to cooperate and complement each other.

Programming paradigm = Language sense

There are several ways to learn knowledge: one by memory, one by practice, and one by cultivation. Take the English study to say, learns the word, only by the memory can, learns the sentence pattern, the grammar, the light memory is not enough, needs the practice to practise to make perfect, but must speak the authentic English, the light memory and the practice is not enough. From elementary school to university, even doctoral graduate, in addition to English majors, most people have practiced English for ten or twenty years, how about their level? You're welcome, but objectively speaking: a word, rotten.

There is only one reason, that is, the domestic English teaching method is a serious blunder. Teaching always revolves around words, phrases, sentence patterns, grammar, lack of attention to the sense of language and training, resulting in students will only ' Chinglish '. Similarly, a C programming person may soon be able to write some C + + programs, but if he only focus on C + + syntax and do not focus on the development of OOP language sense, then write the program must be ' C-C + + '. It is better to use C instead of the other way round. ”

Bottom line: Learning programming paradigm can enhance the language sense of programming languages.

The sense of language is a person's keen perception of speech, reflecting his ability to grasp the intuition of the whole language. The strong sense of language, can listen to the overtones, can say puns, can read meaningful work, can write accrue of the text. This is a kind of comprehensive quality and accomplishment, its importance is self-evident. So how to cultivate the sense of language? Ordinary learning and training can not be less, but if the cultural background and thinking behind the language is impossible. Programming paradigm is the embodiment of the way of thinking of programming, and therefore is the key to cultivate the language sense of programming languages.

The sense of language has, those design patterns, frameworks, and even architecture, and other seemingly mysterious things, will naturally come.

Example of using yield to implement a co-process operation

Import Timeimport queuedef Consumer (name):    print ("--->starting eating baozi ...") while    True:        new_ Baozi = yield        print ("[%s] is eating Baozi%s"% (Name,new_baozi))        #time. Sleep (1) def producer ():     r = Con.__nex t__ ()    r = con2.__next__ ()    n = 0 while    N < 5:        n +=1        con.send (n)        con2.send (n)        print ("\ 033[32;1m[producer]\033[0m is making Baozi%s "%n)  if __name__ = = ' __main__ ':    con = consumer (" C1 ")    Con2 = Consumer ("C2")    p = producer ()

What conditions can be called the Association Process:

    1. Concurrency must be implemented in only one single thread
    2. No lock required to modify shared data
    3. The context stack in the user program that holds multiple control flows
    4. A coprocessor encounters an IO operation that automatically switches to other co-threads

Based on the above 4-point definition, the process we have just implemented with yield is not considered a qualified thread.

Greelet refers to a pseudo-concurrency mechanism that uses a task scheduler and some generators or processes to implement collaborative user-space multithreading, called Micro-Threading.

The main idea of the Greelet mechanism is that the yield statement in the generator function or the co-function will suspend the execution of the function until later using the next () or send () operation to recover. You can use a scheduler loop to collaborate on multiple tasks between a set of generator functions.

Several basic network I/O models for the network framework:

Blocked single-threaded: This is the most basic I/O model, and the next request is processed only after a request has been processed. Its disadvantage is poor performance, and if a request is blocked, it will make the service unable to continue to accept the request. However, this model is relatively simple to write code, and is very suitable when dealing with a situation where access is small.

Blocking Multithreading: For the disadvantage of a limited number of single-threaded accept requests, a natural idea is to open a thread for each request. The advantage of this is the ability to accept more requests, the disadvantage is that after a certain number of threads generated, the process requires a lot of switching context between the operation, will occupy a large amount of CPU time, but so the processing of the code is slightly higher than the case of a single process.

Non-blocking event-driven: In order to solve the problem of multithreading, one approach is to use a loop to check if there is a network IO event occurring in order to decide how to handle it (reactor design mode). The benefit of doing so is to further reduce the CPU's resource consumption. The disadvantage is that this makes the program difficult to write, because the process of request acceptance is determined by reactor, which makes the execution process of the program difficult to grasp. When a request is received, if a blocking operation is involved, the processing of the request stops to accept another request, and the process of executing the program is not as intuitive as a linear program. The twisted framework is a typical example of applying this IO model.

Non-blocking Coroutine: This mode is designed to solve the problem that the event-driven model is not intuitive, it is event-driven in nature, and adds the concept of coroutine.

The difference from thread/process

Threads are preemptive scheduling, multiple threads are executed in parallel to preempt common system resources, and micro-threading is a cooperative scheduling.

In fact, Greenlet is not a real concurrency mechanism, but rather within the same thread, switching between the execution blocks of different functions, implementing "You run for a while, I run for a while", and when you switch, you must specify when to switch and where to switch to. The Greenlet interface is relatively easy to use, but the way of thinking when using greenlet differs from other concurrency scenarios:

1. The threading/process model is usually considered from a concurrency perspective, separating the tasks that can be processed in parallel and worthy of parallel processing, running under different threads/processes, and then considering the mutually exclusive and conflicting issues that can be caused by the separation process, and ensuring the correctness of concurrent processing with mutually exclusive resource lock protection.

2. Greenlet is required to avoid blocking from the perspective of development, when blocking, the explicit switch to another piece of code that is not blocked execution, until the original blocking condition disappears, and then manually switch back to the original code snippet to continue processing. Therefore, the essence of Greenlet is a reasonably arranged serial.

3. Greenlet essence is serial, so in the absence of an explicit switch, the other parts of the code can not be executed, if you want to avoid the code for a long time to occupy the computational resources cause the program to feign animation, it is still necessary to use the Greenlet and thread/process mechanism (each thread, Multiple Greenlet can be established under a process, but cannot be toggled or communicated across threads/processes Greenlet.

Use

A "Greenlet" is a very small independent micro-thread. You can think of it as a stack frame, the bottom of which is the initial call, and the top of the stack is the current Greenlet pause position. You use Greenlet to create a bunch of these stacks and then jump between them to execute. Jumps are not absolute: a greenlet must choose to jump to another greenlet that is selected, which causes the previous one to hang and the other to recover. A jump between two Greenlet is called a toggle (switch).

When you create a greenlet, it gets an initialized empty stack; When you first switch to it, he starts the specified function and then switches out of Greenlet. When the final stack-bottom function ends, the Greenlet stack is programmed empty, and Greenlet is dead. Greenlet will also die because of an unchecked anomaly.

Example: sample from Official document

From Greenlet import Greenletdef test1 ():   print   gr2.switch ()   print 34def test2 ():   print   Gr1.switch ()   Print 78gr1 = Greenlet (test1) GR2 = Greenlet (test2) Gr1.switch ()



The last line jumps to Test1 (), it prints 12, then jumps to test2 (), prints 56, then jumps back to test1 (), prints 34, then test1 () ends, Gr1 dies. Execution then returns to the original Gr1.switch () call. Note that 78 is not printed, because GR1 is dead and will not be switched.

Greenlet-based framework

Eventlet

Eventlet is a network application-oriented concurrency processing framework based on Greenlet, which provides APIs that are very similar to other Python threads and process models, such as "thread" pools, queues, and provides an ultra-lightweight concurrency adaptation approach to the Python release's own libraries and other modules. It is much more convenient than using Greenlet directly.

The basic principle is to adjust the Python socket call and switch to other Greenlet execution when blocking occurs, thus ensuring the efficient use of resources. It is important to note that:

The function provided by Eventlet can only handle socket calls in Python code, and cannot modify the socket invocation of the C language part of the module. For the latter type of module, it is still necessary to encapsulate the code of the calling module in a Python standard thread call, and then use the adapter provided by Eventlet to implement Eventlet collaboration with standard threads.

While Eventlet encapsulates the API as a very similar form of standard line libraries, the actual concurrent execution flow between the two is still significantly different. In the absence of I/O blocking, unless explicitly declared, the currently executing Eventlet never hands the CPU to the other Eventlet, while the standard thread is always competing for running resources, regardless of whether it is blocked or not. All Eventlet are largely not helpful for the time-consuming operation of large operations that are not related to I/O blocking.

Gevent

Gevent is a coroutine-based Python network function library that provides a high-level concurrency API at the top of the Libev event loop by using Greenlet.

The main features are as follows:

Based on Libev's Fast event loop, Linux above is the epoll mechanism

Greenlet-based lightweight execution unit

API re-uses the contents of the Python standard library

SSL-enabled collaborative sockets

DNS queries can be implemented via thread pool or c-ares

Enables third-party modules to become collaborative with monkey patching functionality



About the epoll mechanism for Linux:

Epoll is an improved poll of the Linux kernel for processing large-volume file descriptors, an enhanced version of the multiplexed IO interface select/poll under Linux, which significantly increases the CPU utilization of the program in cases where there are only a few active instances of a large number of concurrent connections. Advantages of Epoll:

Supports a process to open a large number of socket descriptors. The FD opened by a process of select is qualified by the Fd_setsize setting, and Epoll does not have this limit, and it supports the maximum number of open files, much greater than 2048.

IO efficiency does not decrease linearly with the number of FD: Since Epoll only operates on the "active" socket, only the "active" socket will take the initiative to invoke the callback function, and the other idle sockets will not.

Use Mmap to speed up the kernel and user space message delivery. Epoll is implemented by the kernel in user space mmap the same piece of memory.

Kernel fine tuning.

Libev mechanism

Provides a mechanism for invoking a callback function when a file descriptor event occurs. Libev is an event circulator that registers an event of interest to Libev, such as a socket-readable event, and Libev manages the source of the registered event and triggers the corresponding program when the event occurs.

Examples in the official documentation:

>>> Import gevent>>> from gevent import socket>>> urls = [' www.google.com.hk ', ' www.example.com ', ' www.python.org '  ]>>> jobs = [Gevent.spawn (socket.gethostbyname, url) for URL in URLs] >>> Gevent.joinall (Jobs, timeout=2) >>> [job.value for job in jobs][' 74.125.128.199 ', ' 208.77.188.166 ', ' 82.94.164.162 ']



Note: the Gevent.spawn () method spawn some jobs and then, through Gevent.joinall, joins the jobs into the micro-thread execution queue to wait for it to complete, setting the timeout to 2 seconds. The results after execution are checked by gevent. Greenlet.value values to collect. The Gevent.socket.gethostbyname () function has the same interface as the standard Socket.gethotbyname (), but it does not block the entire interpreter, so that other greenlets can be executed following an unimpeded request.

Monket Patching

Python's running environment allows us to modify most of the objects at run time, including modules, classes, and even functions. While this creates an "implicit side effect" and the problem is difficult to debug, Monkey patching comes in handy when you need to modify the underlying behavior of Python itself. Monkey patching enables gevent to modify most of the blocking system calls in the standard library, including modules such as socket,ssl,threading and select, and become collaborative.

>>> from gevent import monkey;

>>> Monkey. Patch_socket ()

>>> Import Urllib2

With the Monkey.patch_socket () method, the Urllib2 module can be used in a multi-micro threading environment to achieve the purpose of working with Gevent.

Event Loops

Unlike other network libraries, like Gevent and Eventlet, the event loop is implicitly started in a greenlet. There is no need to call the reactor (reactor) of run () or dispatch (), which is reactor in twisted. When Gevent's API function wants to block, it gets the hub instance (the Greenlet of the execution time loop) and switches the past. If there are no hub instances, they are created dynamically.

The event loop provided by Libev uses the system's fastest polling mechanism by default, setting the LIBEV_FLAGS environment variable to specify the polling mechanism. Libev_flags=1 is Select, Libev_flags = 2 is poll, Libev_flags = 4 is epoll,libev_flags = 8 is kqueue.

The Libev API is located under Gevent.core. Note that the callback for the Libev API runs on the hub's Greenlet, so use the synchronous Greenlet API. You can use asynchronous APIs such as Spawn () and Event.set ().

Python's process and IO operations

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.