Python 3.x Learning note 17 (co-process and I/O mode)

Source: Internet
Author: User

1. Co-process (micro-threading)
A process is a lightweight thread that is user-state.
The co-process has its own register context and stack. When the schedule is switched, the register context and stack are saved elsewhere, and the previously saved register context and stack are restored when it is cut back. So:

The process can retain the state of the last invocation (that is, a specific combination of all local states), each time the procedure is re-entered, which is equivalent to the state of the last call, in other words: The position of the logical stream at the last departure.

2.greenlet Module
Greenlet is a C-implemented Coprocessor module that allows you to switch freely between any function, rather than declaring the function as generator, compared to the yield that comes with python.

Example

 from Import Greenlet def fun1 ():     Print (6)     # Convert to Gar2    Print (+) def fun2 ():     Print (Wu)     # start gar2 = Greenlet (fun2) gar1.switch ()

3.gevent Module
Gevent is a third-party library that makes it easy to implement concurrent or asynchronous programming through Gevent, and the main pattern used in Gevent is Greenlet, which is a lightweight coprocessor that accesses Python in the form of a C extension module. Greenlet all run inside the main program operating system process, but they are dispatched in a collaborative manner.

Importgeventdeffun1 ():Print('run Fun1 for the first time') Gevent.sleep (2) #切换到fun2的gevent. Sleep (1) This stepPrint('Second run fun1')deffun2 ():Print('run Fun2 for the first time') Gevent.sleep (1) #sleep时间没到继续切换到fun3的gevent. Sleep (2)Print('Second run fun2')deffun3 ():Print('run Fun3 for the first time') Gevent.sleep (2)    Print('Second run Fun3') Gevent.joinall ([Gevent.spawn (FUN1), Gevent.spawn (fun2), Gevent.spawn (FUN3),])

Results

First run FUN1 first run fun2 first run FUN3 second run fun2 second run FUN1 second run Fun3

4.gevent urllib I/O operation is not detected by default

5. To operate the crawler asynchronously, you must add Monkey.patch_all (), which means that all IO operations of the current program are marked separately.

Such as

 fromUrllibImportRequestImportGevent,time fromGeventImportMonkeymonkey.patch_all ()#Mark All IO operations of the current program individuallydeff (URL):Print('get%s'%URL) Resp=request.urlopen (URL) data=Resp.read ()Print('%d data received from%s.'%(len (data), URL)) start_time=time.time () Gevent.joinall ([Gevent.spawn (F,'https://www.python.org/'), Gevent.spawn (F,'https://www.baidu.com/'), Gevent.spawn (F,'https://github.com/'),])Print('Total time:', Time.time ()-start_time)

6. Event-driven model
Most of the current UI programming is an event-driven model, as many UI platforms provide the OnClick () event, which represents the mouse down event. The event-driven model is broadly thought of as follows:
1). There is an event (message) queue;
2. When the mouse is pressed, add a click event (message) to this queue;
3). There is a loop that constantly pulls events out of the queue, depending on the event, calling different functions, such as onclick (), OnKeyDown (), etc.;
4). Events (messages) generally each save their own handler pointers, so that each message has its own handler function;


7. Event-driven programming is a programming paradigm where the execution flow of a program is determined by an external event. It is characterized by the inclusion of an event loop that uses a callback mechanism to trigger the corresponding processing when an external event occurs. Two other common programming paradigms are (single-threaded) synchronization and multithreaded programming.


8. Cache I/O

Cache I/O is also known as standard I/O, and most file system default I/O operations are cache I/O. In the Linux cache I/O mechanism, the operating system caches the I/O data in the file system's page cache, which means that the data is copied into the buffer of the operating system kernel before it is copied from the operating system kernel buffer to the application's address space.

Disadvantages of cache I/O:
Data is required to perform multiple copies of data in the application address space and the kernel during transmission, and the CPU and memory overhead of these data copy operations is very large.

Note: I/O for this cache I/O in a Linux environment
Explanation: http://www.cnblogs.com/alex3714/articles/5876749.html

9.IO mode

Blocking I/O (blocking IO)
Non-blocking I/O (nonblocking IO)
I/O multiplexing (IO multiplexing)
Signal-driven I/O (signal driven IO)
asynchronous I/O (asynchronous IO)

Python 3.x Learning note 17 (co-process and I/O mode)

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.