1.手动协程操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
# pip install gevent from greenlet import greenlet def test(): print ( 'He ' ) gr2.switch() # 切换到test2 print ( 'a ' ) gr2.switch() def test2(): print ( 'is ' ) gr1.switch() print ( 'student.' ) gr1 = greenlet(test) # 创建一个协程 gr2 = greenlet(test2) gr1.switch() # 切换到gr1,也就是运行它 |
2. 自动协程,遇到I/O自动切换
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import gevent from gevent import monkey; monkey.patch_all() # 导入monkey,给所有支持的模块打上补丁,变成非阻塞模块,也就是使其能够进行协程操作 def test1(n): print ( "I'm doing my homework." ) gevent.sleep(n) # 模拟I/O操作,gevent自动切换 print ( 'Keep doing my homework.' ) def test2(n): print ( "I have no time to watch TV." ) gevent.sleep(n) print ( 'Stop thinking!' ) def test3(): print ( "Life is hard." ) # 等待 greenlets 全部结束 gevent.joinall([ gevent.spawn(test1, 3 ), # 创建一个新的greeenlet对象,并规划它去执行 test1(3) gevent.spawn(test2, 1 ), gevent.spawn(test3), ]) |
结果:
I'm doing my homework.
I have no time to watch TV.
Life is hard.
Stop thinking!
Keep doing my homework.
3. socket 协程实例:
服务器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import socket import gevent from gevent import monkey monkey.patch_all() # 打补丁,让所有支持的模块变成非阻塞的方法 def server(ip,port): s = socket.socket() # 生成socket对象 s.bind((ip, port)) # 绑定ip和端口 s.listen() # 设置监听 while True : cli, addr = s.accept() # 等待连接 gevent.spawn(handle_request, cli) # 对每一个连接,使用协程的方法生成greenlet def handle_request(conn): try : while True : data = conn.recv( 1024 ) # 接收数据 print ( "recv:" , data) new = 'Hello ' + data.decode().title() conn.send(new.encode( 'utf8' )) # 返回数据 if not data: conn.shutdown(socket.SHUT_WR) except Exception as e: print (e) finally : conn.close() if __name__ = = '__main__' : server( 'localhost' , 9999 ) |
客户端:
1
2
3
4
5
6
7
8
9
10
|
import socket client = socket.socket() client.connect(( 'localhost' , 9999 )) while True : con = input ( '>>>:' ).strip() if len (con) = = 0 : continue client.send(con.encode( 'utf-8' )) data = client.recv( 1024 ) print (data.decode()) client.close() |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/wztshine/p/12054120.html