python -- preliminary knowledge of synergetic process


The operation of thread and process is triggered by program, and the last executor is system; the operation of cooperation program is programmer.

The significance of the existence of the cooperation process: for multithreaded applications, the CPU switches the execution between threads by slicing, which takes time (save state, continue next time). For a co program, only one thread is used to specify the execution order of a code block in one thread.

Scenario of cooperation: when there are a large number of operations (IO) without CPU in the program, it is applicable to cooperation;

event loop is the control point for the execution of a collaboration. If you want to execute a collaboration, you need to use them.

event loop provides the following features:

1. Register, execute and cancel delay call (asynchronous function)
2. Create client and server protocols (tools) for communication
3. Create subprocesses and Protocols (tools) to communicate with other programs
4. Send function calls to thread pool

Example 1:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 """
 4 Example of Association
 5 """
 6 import asyncio
 7 
 8 async def test1():
 9     print("test1, starting")
10     await test2()
11     print("test1, ending")
12 
13 async def test2():
14     print("test2 start")
15 
16 loop = asyncio.get_event_loop()  #asyncio.get_event_loop()  : asyncio Start default event loop
17 loop.run_until_complete(test1())  #run_until_complete()  :  This function is blocked until all asynchronous functions are completed,
18 loop.close()  #close()  :  Close event loop. 

 

Example of greenlet process:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 """
 4 Example of Association
 5 """
 6 import greenlet
 7 
 8 def fun1():
 9     print("12")
10     f2.switch()
11     print("56")
12     f2.switch()
13 
14 def fun2():
15     print("34")
16     f1.switch()
17     print("78")
18 
19 
20 f1 = greenlet.greenlet(fun1)
21 f2 = greenlet.greenlet(fun2)
22 
23 f1.switch()

 

Example of gevent process:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 """
 4 gevent Example of Association
 5 """
 6 import gevent
 7 
 8 def fun1():
 9     print("www.baidu.com")  #First step
10     gevent.sleep(0)
11     print("end the baidu.com")  #The third step
12 
13 def fun2():
14     print("www.yusheng.com")  #The second step
15     gevent.sleep(0)
16     print("end the yusheng.com")  #The fourth step
17 
18 gevent.joinall([
19     gevent.spawn(fun1),
20     gevent.spawn(fun2),
21 ])

 

Example 3: automatic switching in case of IO operation:

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 """
 4 encounter IO Operation Auto Switch:
 5 """
 6 import gevent
 7 import requests
 8 
 9 def fun(url):
10     print("get: %s" % url)
11     gevent.sleep(0)
12     data = requests.get(url)
13     ret = data.text
14     print(url, len(ret))
15 
16 gevent.joinall([
17     gevent.spawn(fun, 'https://www.python.org/'),
18     gevent.spawn(fun, 'https://www.yahoo.com/'),
19     gevent.spawn(fun, 'https://github.com/'),
20 ])

Tags: Python github

Posted on Sat, 09 Nov 2019 06:15:25 -0800 by Lee W