python watchdog asyncio_python学习笔记 异步asyncio

asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持。

asyncio的编程模型就是一个消息循环。我们从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程扔到EventLoop中执行,就实现了异步IO。

用asyncio实现Hello world代码如下:

importasyncio

@asyncio.coroutinedefhello():print("Hello world!")#异步调用asyncio.sleep(1):

r = yield from asyncio.sleep(1)print("Hello again!")#获取EventLoop:

loop =asyncio.get_event_loop()#执行coroutine

loop.run_until_complete(hello())

loop.close()

@asyncio.coroutine把一个generator标记为coroutine类型,然后,就把这个coroutine扔到eventloop中去执行

hello()会先打印出helloworld,然后yield from可以让我们方便的调用另一个generator,由于asyncio.sleep(1)也是一个coroutine

所以线程不会等待asyncio.sleep而是直接中断并执行下一个消息循环,当asyncio.sleep返回的时候,线程就在yield from拿到返回值,此处是None

然后执行下一个语句

把asyncio.sleep(1)看成是一个耗时1秒的IO操作。在此期间主线程没有等待,而是去执行eventloop其他可以执行的coroutine因此可以实现并发执行

接下来封装2个coroutine试试

importthreadingimportasyncio

@asyncio.coroutinedefhello():print('Hello world! (%s)' %threading.currentThread())yield from asyncio.sleep(1)print('Hello again! (%s)' %threading.currentThread())

loop=asyncio.get_event_loop()

tasks=[hello(), hello()]

loop.run_until_complete(asyncio.wait(tasks))

loop.close()

Hello world! (<_mainthread started>)

Hello world! (<_mainthread started>)

(暂停约1秒)

Hello again! (<_mainthread started>)

Hello again! (<_mainthread started>)

由打印的当前线程名称可以看出,两个coroutine是由同一个线程并发执行的。

如果把asyncio.sleep()换成真正的IO操作,则多个coroutine就可以由一个线程并发执行。

我们用asyncio的异步网络连接来获取sina、sohu和163的网站首页:

importasyncio

@asyncio.coroutinedefwget(host):print('wget %s...' %host)

connect= asyncio.open_connection(host, 80)

reader, writer= yield fromconnect

header= 'GET / HTTP/1.0\r\nHost: %s\r\n\r\n' %host

writer.write(header.encode('utf-8'))yield fromwriter.drain()whileTrue:

line= yield fromreader.readline()if line == b'\r\n':break

print('%s header > %s' % (host, line.decode('utf-8').rstrip()))#Ignore the body, close the socket

writer.close()

loop=asyncio.get_event_loop()

tasks= [wget(host) for host in ['www.sina.com.cn', 'www.sohu.com', 'www.163.com']]

loop.run_until_complete(asyncio.wait(tasks))

loop.close()

wget www.sohu.com...

wget www.sina.com.cn...

wget www.163.com...

(等待一段时间)

(打印出sohu的header)

www.sohu.com header> HTTP/1.1 200OK

www.sohu.com header> Content-Type: text/html

...

(打印出sina的header)

www.sina.com.cn header> HTTP/1.1 200OK

www.sina.com.cn header> Date: Wed, 20 May 2015 04:56:33GMT

...

(打印出163的header)

www.163.com header > HTTP/1.0 302Moved Temporarily

www.163.com header > Server: Cdn Cache Server V2.0

asyncio提供了完善的异步IO支持;

异步操作需要在coroutine中通过yield from完成;

多个coroutine可以封装成一组Task然后并发执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值