异步IO模块的使用

Posted jintian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了异步IO模块的使用相关的知识,希望对你有一定的参考价值。

gevent:mokey.path_all  (找到所有socket,进行异步IO)

gevent+requests=》grequests

Twisted: reactor.run() 死循环

Tornado

=》gevent > Twisted > Tornado > asyncio

www.cnblogs.com/wupeiqi/articles/6229292.html

技术图片
"""
import gevent

import requests
from gevent import monkey
monkey.patch_all()


def task(method, url, req_kwargs):
    print(method, url, req_kwargs)
    response = requests.request(method=method, url=url, **req_kwargs)
    print(response.url, response.content)

# ##### 发送请求 #####
# gevent.joinall([
#     gevent.spawn(task, method=‘get‘, url=‘https://www.python.org/‘, req_kwargs={}),
#     gevent.spawn(task, method=‘get‘, url=‘https://www.yahoo.com/‘, req_kwargs={}),
#     gevent.spawn(task, method=‘get‘, url=‘https://github.com/‘, req_kwargs={}),
# ])
# ##### 发送请求(协程池控制最大协程数量) #####
from gevent.pool import Pool
pool = Pool(5)
gevent.joinall([
    pool.spawn(task, method=‘get‘, url=‘https://www.python.org/‘, req_kwargs={}),
    pool.spawn(task, method=‘get‘, url=‘https://www.yahoo.com/‘, req_kwargs={}),
    pool.spawn(task, method=‘get‘, url=‘https://www.github.com/‘, req_kwargs={}),
])
"""
import grequests

import grequests


request_list = [
    grequests.get(http://httpbin.org/delay/1, timeout=0.001),
    grequests.get(http://fakedomain/),
    grequests.get(http://httpbin.org/status/500)
]


# ##### 执行并获取响应列表 #####
response_list = grequests.map(request_list,size=5)
print(response_list)
gevent.py
技术图片
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from twisted.internet import defer
from twisted.web.client import getPage
from twisted.internet import reactor

def one_done(arg):
    print(arg)

def all_done(arg):
    print(done)
    reactor.stop()

@defer.inlineCallbacks
def task(url):
    res = getPage(bytes(url, encoding=utf8)) # 发送Http请求
    res.addCallback(one_done)
    yield res

url_list = [
    http://www.cnblogs.com,
    http://www.cnblogs.com,
    http://www.cnblogs.com,
    http://www.cnblogs.com,
]

defer_list = [] # [特殊,特殊,特殊(已经向url发送请求)]
for url in url_list:
    v = task(url)
    defer_list.append(v)

d = defer.DeferredList(defer_list)
d.addBoth(all_done)


reactor.run() # 死循环
Twisted.py
技术图片
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tornado.httpclient import AsyncHTTPClient
from tornado.httpclient import HTTPRequest
from tornado import ioloop

COUNT = 0
def handle_response(response):
    global COUNT
    COUNT -= 1
    if response.error:
        print("Error:", response.error)
    else:
        print(response.body)
        # 方法同twisted
        # ioloop.IOLoop.current().stop()
    if COUNT == 0:
        ioloop.IOLoop.current().stop()

def func():
    url_list = [
        http://www.baidu.com,
        http://www.bing.com,
    ]
    global COUNT
    COUNT = len(url_list)
    for url in url_list:
        print(url)
        http_client = AsyncHTTPClient()
        http_client.fetch(HTTPRequest(url), handle_response)


ioloop.IOLoop.current().add_callback(func)
ioloop.IOLoop.current().start() # 死循环
Tornado.py

技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片技术图片

以上是关于异步IO模块的使用的主要内容,如果未能解决你的问题,请参考以下文章

使用C++ boost从零构建一个异步文件IO系统

python的异步IO模块

# 进程/线程/协程 # IO:同步/异步/阻塞/非阻塞 # greenlet gevent # 事件驱动与异步IO # SelectPollEpoll异步IO 以及selectors模块 # (示

Py修行路 socket + select 实现 异步IO模块

Python学习---IO的异步[tornado模块]

自定义异步IO模块开发