python 高性能编程 异步并发编程 asyncio 协程笔记暂记

python 高性能编程 异步并发编程 asyncio

异步并发编程-相关概念

# 输出3个“i=3”
for (var i = 0; i < 3; i++) {
	setTimeout(function () {
		console.log("i =", i)
	}, 100)
}
  • 多线程:实现异步的一种方法:python多线程threading
  • 协程:协程也是实现异步的一种方法。函数执行到一半,切换到另一个函数,在一个线程中,asyncio可以实现在io等待的过程中执行其他函数。
pip install greenlet
from greenlet import greenlet
def test1():
    print 12
    gr2.switch()
    print 34

def test2():
    print 56
    gr1.switch()
    print 78

gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()

异步并发编程-相关概念

# 协程函数的执行方法的“格式”
asyncio.get_event_loop().run_until_complete(main())

asyncio 遇到io自动切换

import asyncio

@asyncio.coroutine
def func1():
    print(1)
    yield from asyncio.sleep(2)  # 遇到IO耗时操作,自动化切换到tasks中的其他任务
    print(2)


@asyncio.coroutine
def func2():
    print(3)
    yield from asyncio.sleep(2) # 遇到IO耗时操作,自动化切换到tasks中的其他任务
    print(4)


tasks = [
    asyncio.ensure_future( func1() ),
    asyncio.ensure_future( func2() )
]

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

结果: 1 3 2 4

async await 关键字(遇到async的函数是,执行到await的耗时部分停止,去执行其他函数,当await的耗时部分执行完成只后再继续执行async标记的函数)

import asyncio

@asyncio.coroutine
def func1():
    print(1)
    yield from asyncio.sleep(2)  # 遇到IO耗时操作,自动化切换到tasks中的其他任务
    print(2)


@asyncio.coroutine
def func2():
    print(3)
    yield from asyncio.sleep(2) # 遇到IO耗时操作,自动化切换到tasks中的其他任务
    print(4)


tasks = [
    asyncio.ensure_future( func1() ),
    asyncio.ensure_future( func2() )
]

loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

网络部分

aiohttp

https://docs.aiohttp.org/en/stable/
每天学Python-通过aiohttp模块实现HTTP高并发处理

self._session = aiohttp.ClientSession(loop=self._loop, timeout=aiohttp.ClientTimeout(total=10))

self._websocket: Optional[aiohttp.ClientWebSocketResponse] = None

 message: aiohttp.WSMessage
                    async for message in websocket:
                        await self._on_ws_message(message)
                        # 至少成功处理1条消息
                        retry_count = 0
async def run_multi_client():
    clients = [blivedm.BLiveClient(room_id) for room_id in TEST_ROOM_IDS]
    handler = MyHandler()
    for client in clients:
        client.add_handler(handler)
        client.start()

    try:
        await asyncio.gather(*(
            client.join() for client in clients
        ))
async def main():
    await run_single_client()
    await run_multi_client()

handler - urllib的open方法

基础用法

import urllib.request
url = 'http://www.baidu.com'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36'}

#(1)创建handler对象
handler = urllib.request.HTTPHandler()
#(2) opener对象,不适用urlopen方法了
opener = urllib.request.build_opener(handler)
# (3)构建请求对象
request = urllib.request.Request(url=url,headers=headers)
#(4)调用open方法,发送请求
response = opener.open(request)

content = response.read().decode('utf-8')
print(content)

代理(中介)

         使用多个代理来访问服务器。(代理需要代理云服务提供商提供,再有了代理ip和端口只后,可以再电脑中设置代理:浏览器设置=》高级设置=》代理设置=》局域网=》为LAN使用代理,试用一下)

import urllib.request

url = 'http://*'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36'}

proxies = {'http':'ip:port'}
handler = urllib.request.ProxyHandler(proxies = proxies)
opener = urllib.request.build_opener(handler)
request = urllib.request.Request(url=url,headers=headers)
response = opener.open(request)
content = response.read().decode('utf-8')

cookie 模拟登录

import urllib.request
import urllib.parse
import http.cookiejar
cookie =http.cookiejar.CookieJar()
handler = urllib.request.HttpCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)

posturl = 'http://*'
formdata = {'*': '*','*': '*','*': '*','*': '*','*': '*'}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36'}

request = urllib.request.Request(url=posturl,headers=headers)
formdata = urllib.parse.urlencode(formdata).encode()
response = opener.open(request,data = formdata )
# content = response.read().decode('utf-8')
get_url = 'http://*'
request = urllib.request.Request(url=get_url ,headers=headers)
response = opener.open(request)
content = response.read().decode('utf-8')
print(content)

参考与更多

asyncio异步并发部分

library-asyncio
视频
笔记

更多

LOG & handler

Handler作为logging模块中最常用的class之一, 通过了解其实现, 能够更好的帮助我们理解logging模块.

python中handler的作用是什么

Python 模块之Logging(四)——常用handlers的使用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值