Python Learning Notes-co-process

Source: Internet
Author: User

Python controls concurrency, In addition to multithreading and multi-process, you can also use the CO-COROUTINE. He switches from one thread to another through the Programmer's human Designation.


Example 1: a switch () can be used to move the person back and forth to another function; when all functions are finished, the final output print (10)

#!/usr/bin/env python#-*-coding:utf-8-*-# author:alex lifrom greenlet import greenletdef test1 (): print (+) gr2.s Witch () Print (gr2.switch) print (' 9 ') def test2 (): print (gr1.switch) () print (+) gr1.switch () gr 1 = greenlet (test1) GR2 = Greenlet (test2) gr1.switch () Print (ten)--------12563478910


Example 2, in addition to the Greenlet module we can also use the Gevent module

Gevent is a third-party library, through the Greenlet implementation of the process, the basic idea is:

When an greenlet encounters an IO operation, such as accessing the network, it automatically switches to the other greenlet, waits until the IO operation is complete, and then switches back to execution at the appropriate Time. Because the IO operation is very time-consuming and often puts the program in a waiting state, with gevent automatically switching the co-process for us, it is guaranteed that there will always be greenlet running, rather than waiting for io.

Since the switchover is done automatically during IO operations, gevent needs to modify some of the standard libraries that Python comes with, which is done at startup with Monkey Patch:


For example: first import gevent and Monkey patches, then define a function f, Gevent.spawn's main function is to produce greenlet objects, then he will automatically switch internally, when the first print command is executed, because it will block, it will automatically switch to other Greenlet objects to perform the operation, and then switch back to get the data results.

#!/usr/bin/env python# -*- coding:utf-8 -*-from gevent import monkey;  Monkey.patch_all () import geventimport requestsdef f (url):     print (' GET:  %s '  % url)     resp = requests.get (url)      Data = resp.text    print ('%d bytes received from %s. '   %  (len (data),  url)) gevent.joinall ([        gevent.spawn (f,   ' https://www.python.org/'),         gevent.spawn (f,  ' https:/ /www.yahoo.com/'),         gevent.spawn (f,  ' https://github.com/'), ])----------"c:\program files\python3\python.exe"  c:/temp/s13day11/day11/s18.pyget: https:// Www.python.org/GET: https://www.yahoo.com/GET: https://github.com/47413 bytes received  from https://www.python.org/.24810 bytes received from https://github.com/.450576 bytes  received from https://www.yahoo.com/.


This article is from the "mapo tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1869198

Python Learning Notes-co-process

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.