35 协程

1.生成器版的任务切换

import time

def jishi1hao():
    for i in range(11):
        # print('到这里来!')
        yield   #帮我们记录了任务的执行状态
        time.sleep(1)
        print('%s号客户2秒就搞定'%i)

def jishi2hao():
    g = jishi1hao()  #获得生成器
    next(g)
    for i in range(10):
        time.sleep(1)
        print('%s号技师,正在忙'%i)
        next(g)

# jishi1hao()
jishi2hao()
生成器版任务切换

2.greenlet 模块实现任务切换

import time
import greenlet

def func1():
    time.sleep(2)
    print('约吗?')
    g2.switch()
    time.sleep(2)
    print('不约')
    g2.switch()

def func2():
    time.sleep(2)
    print('你好')
    g1.switch()
    time.sleep(2)
    print('你不好')

g1 = greenlet.greenlet(func1)
g2 = greenlet.greenlet(func2)

g1.switch()
greenlet模块实现任务切换

3.单线程下,通过协程异步提交任务

from gevent import monkey;monkey.patch_all()
import time
import gevent

def func1(n):
    print('xxxxxx',n)
    # gevent.sleep(2)
    time.sleep(2)
    print('cccccc',n)

def func2(m):
    print('111111',m)
    # gevent.sleep(2)
    time.sleep(2)
    print('222222',m)

start_time = time.time()
g1 = gevent.spawn(func1,'alex')
g2 = gevent.spawn(func2,'德玛西亚')
# g1.join() #
# g2.join()
gevent.joinall([g1,g2])
end_time = time.time()
print(end_time - start_time)

print('代码结束')
gevent协程实现并发

4.非IO阻塞模型应用-单线程下实现多人聊天

import time
import socket

server = socket.socket()

ip_port = ('127.0.0.1',8001)
server.bind(ip_port)

server.listen()
server.setblocking(False)
conn_list = []

while 1:
    while 1:
        try:
            conn,addr = server.accept()
            conn_list.append(conn)
            break
        except BlockingIOError:
            time.sleep(0.1)
            print('此时还没有人链接我')

    for sock in conn_list:
        print(sock)
        while 1:
            try:
                from_client_msg = sock.recv(1024)
                print(from_client_msg.decode('utf-8'))
                sock.send(b'hello')
                break
            except BlockingIOError:
                print('还没有任何的消息啊')
非阻塞IO模型服务端
import socket
client = socket.socket()

client.connect(('127.0.0.1',8001))
while 1:
    to_server_msg = input('我想对你说>>>>')
    client.send(to_server_msg.encode('utf-8'))
    from_server_msg = client.recv(1024)
    print(from_server_msg.decode('utf-8'))
非阻塞IO模型客户端

5.IO多路复用应用—单线程下实现多人聊天

import select
import socket

server = socket.socket()
server.bind(('127.0.0.1',8001))
rlist = [server,]
server.listen()
while 1:
    print('11111')
    rl,wl,el = select.select(rlist,[],[])
    print(222222)
    print('server对象>>>',server)
    print(rl)
    for sock in rl:
        if sock == server:
            conn,addr = sock.accept()
            rlist.append(conn)
        else:
            from_client_msg = sock.recv(1024)
            print(from_client_msg.decode('utf-8'))

# conn,addr = server.accept()

# from_client_msg = conn.recv(1024)
# print(from_client_msg.decode('utf-8'))
IO多来复用服务端
import socket

client = socket.socket()
client.connect(('127.0.0.1',8001))

to_server_msg = input('发给服务端的消息:')
client.send(to_server_msg.encode('utf-8'))
# from_server_msg = client.recv(1024)
# print(from_server_msg.decode('utf-8'))
IO多路复用客户端

 

posted @ 2018-12-04 20:26  冰底熊  阅读(203)  评论(0编辑  收藏  举报