码迷,mamicode.com
首页 > 其他好文 > 详细

异步IO模块的使用

时间:2019-08-25 01:19:14      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:lse   close   div   ons   git   web   def   callback   rgs   

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模块的使用

标签:lse   close   div   ons   git   web   def   callback   rgs   

原文地址:https://www.cnblogs.com/jintian/p/11406558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!