flask+redis
改成了异步模式aiohttp+aioredis
。scrapy
用来爬取代理,aiohttp
做服务端,存储用redis
。scrapy
部分本身没什么特别的,不过pipeline
部分最后采取了共用aioredis
实现的RedisClient
的写法。__init__(self)
不能是协程,最后采用以下代码实现:@classmethod
async def create(cls):
"""
__init__不能是协程,写成工厂函数
"""
self = cls()
self.pool = await aioredis.create_redis_pool(address=f"redis://{REDIS_HOST}", password=REDIS_PASSWORD, encoding='utf-8')
self.conn = await self.pool
return self
使用方法也在这个文件中附了1个main
函数作为示例:
async def main():
r = await RedisClient.create()
counts = await r.get_count()
print(counts)
return counts
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
aiohttp
基于类的视图进行改写,也没特别值得注意的地方。subprocess
调用另一个文件夹的scrapy
项目,使用了一个cd
类改变工作区文件夹路径:class cd:
"""
改变目前工作区文件夹路径的上下文管理器
"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
使用起来很简单:
with cd("../"):
# 现在进入了上一层目录
pass